Skip to contentSkip to conversation

sched/fair: fix initial util_avg calculation

PATCH Applied Dawei Li 4 participants 9 messages Mar 15, 2024 – May 17, 2024 Scheduler

lore
Share & tools
lore

Dawei Li

author
#0 lore raw

PATCH v2 1/1 sched/fair: fix initial util_avg calculation

Change se->load.weight to se_weight(se) in the calculation for the initial util_avg to avoid unnecessarily inflating the util_avg by 1024 times.

The reason is that se->load.weight has the unit/scale as the scaled-up load, while cfs_rg->avg.load_avg has the unit/scale as the true task weight (as mapped directly from the task's nice/priority value). With CONFIG_32BIT, the scaled-up load is equal to the true task weight. With CONFIG_64BIT, the scaled-up load is 1024 times the true task weight. Thus, the current code may inflate the util_avg by 1024 times. The follow-up capping will not allow the util_avg value to go wild. But the calculation should have the correct logic.

---
Changes in v2:
- update the commit message

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +3 -2
index a19ea290b790..5f98f639bdb9 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *		* se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
 	if (cap > 0) {
 		if (cfs_rq->avg.util_avg != 0) {
-			sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+			sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);
 			sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
 			if (sa->util_avg > cap)
Signature · 1 line
2.40.1
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 14 lines · Dawei Li

Change se->load.weight to se_weight(se) in the calculation for the initial util_avg to avoid unnecessarily inflating the util_avg by 1024 times.

The reason is that se->load.weight has the unit/scale as the scaled-up load, while cfs_rg->avg.load_avg has the unit/scale as the true task weight (as mapped directly from the task's nice/priority value). With CONFIG_32BIT, the scaled-up load is equal to the true task weight. With CONFIG_64BIT, the scaled-up load is 1024 times the true task weight. Thus, the current code may inflate the util_avg by 1024 times. The follow-up capping will not allow the util_avg value to go wild. But the calculation should have the correct logic.

Quoted 1 line
Quoted 33 lines

---
Changes in v2:
- update the commit message

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +3 -3
index a19ea290b790..5f98f639bdb9 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *             * se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
        if (cap > 0) {
                if (cfs_rq->avg.util_avg != 0) {
-                       sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+                       sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);
                        sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
                        if (sa->util_avg > cap)
--

2.40.1

#2 lore raw
In reply to Dawei Li
Quoted 36 lines · Dawei Li

Change se->load.weight to se_weight(se) in the calculation for the initial util_avg to avoid unnecessarily inflating the util_avg by 1024 times.

The reason is that se->load.weight has the unit/scale as the scaled-up load, while cfs_rg->avg.load_avg has the unit/scale as the true task weight (as mapped directly from the task's nice/priority value). With CONFIG_32BIT, the scaled-up load is equal to the true task weight. With CONFIG_64BIT, the scaled-up load is 1024 times the true task weight. Thus, the current code may inflate the util_avg by 1024 times. The follow-up capping will not allow the util_avg value to go wild. But the calculation should have the correct logic.

---
Changes in v2:
- update the commit message

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +2 -1
index a19ea290b790..5f98f639bdb9 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *		* se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such

kernel/sched/fair.c void post_init_entity_util_avg(struct task_struct *p) · line 1082

Quoted 5 lines
kernel/sched/fair.c +1 -1
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
 	if (cap > 0) {
 		if (cfs_rq->avg.util_avg != 0) {
-			sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+			sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);

Hi,

The comment above the declaration of se_weight function says we should be using full load resolution and get rid of this helper.

Should we be adding new user of the helper?

/*
* XXX we want to get rid of these helpers and use the full load resolution. */
static inline long se_weight(struct sched_entity *se)

{
	return scale_load_down(se->load.weight);
}
Quoted 6 lines
kernel/sched/fair.c +0 -0
 			sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
 			if (sa->util_avg > cap)
Signature · 1 line
2.40.1
author
#3 lore raw

Hi Vishal

Thanks for the comment!
Do you suggest using scale_load_down() in place of se_weight()? It's a soft bug we should fix one way or another before what the comment mentions really happens.
I am actually confused that we have both se_weight() and scale_load_down(), and they do the same thing.

Best regards,
Dawei

Quoted 65 lines · Vishal Chourasia
Quoted 36 lines · Dawei Li

Change se->load.weight to se_weight(se) in the calculation for the initial util_avg to avoid unnecessarily inflating the util_avg by 1024 times.

The reason is that se->load.weight has the unit/scale as the scaled-up load, while cfs_rg->avg.load_avg has the unit/scale as the true task weight (as mapped directly from the task's nice/priority value). With CONFIG_32BIT, the scaled-up load is equal to the true task weight. With CONFIG_64BIT, the scaled-up load is 1024 times the true task weight. Thus, the current code may inflate the util_avg by 1024 times. The follow-up capping will not allow the util_avg value to go wild. But the calculation should have the correct logic.

---
Changes in v2:
- update the commit message

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +2 -1
index a19ea290b790..5f98f639bdb9 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *           * se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such

kernel/sched/fair.c void post_init_entity_util_avg(struct task_struct *p) · line 1082

Quoted 5 lines
kernel/sched/fair.c +1 -1
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
      if (cap > 0) {
              if (cfs_rq->avg.util_avg != 0) {
-                     sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+                     sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);

Hi,

The comment above the declaration of se_weight function says we should be using full load resolution and get rid of this helper.

Should we be adding new user of the helper?

/*
* XXX we want to get rid of these helpers and use the full load resolution. */
static inline long se_weight(struct sched_entity *se)

{
        return scale_load_down(se->load.weight);
}
Quoted 6 lines
kernel/sched/fair.c +0 -1
                      sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
                      if (sa->util_avg > cap)
--

2.40.1

#4 lore raw
Quoted 4 lines · Dawei Li

Hi Vishal

Thanks for the comment!
Do you suggest using scale_load_down() in place of se_weight()?

scale_load_down should be better.

Quoted 2 lines

It's a soft bug we should fix one way or another before what the comment mentions really happens.

IIUC, We should be moving towards using full load resolution
for all the calculations. In that case, we need not worry about scaling load at all. Maybe someone could provide context here.

Quoted 72 lines

I am actually confused that we have both se_weight() and scale_load_down(), and they do the same thing.

Best regards,
Dawei

Quoted 65 lines · Vishal Chourasia
Quoted 36 lines · Dawei Li

Change se->load.weight to se_weight(se) in the calculation for the initial util_avg to avoid unnecessarily inflating the util_avg by 1024 times.

The reason is that se->load.weight has the unit/scale as the scaled-up load, while cfs_rg->avg.load_avg has the unit/scale as the true task weight (as mapped directly from the task's nice/priority value). With CONFIG_32BIT, the scaled-up load is equal to the true task weight. With CONFIG_64BIT, the scaled-up load is 1024 times the true task weight. Thus, the current code may inflate the util_avg by 1024 times. The follow-up capping will not allow the util_avg value to go wild. But the calculation should have the correct logic.

---
Changes in v2:
- update the commit message

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +2 -1
index a19ea290b790..5f98f639bdb9 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *           * se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such

kernel/sched/fair.c void post_init_entity_util_avg(struct task_struct *p) · line 1082

Quoted 5 lines
kernel/sched/fair.c +1 -1
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
      if (cap > 0) {
              if (cfs_rq->avg.util_avg != 0) {
-                     sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+                     sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);

Hi,

The comment above the declaration of se_weight function says we should be using full load resolution and get rid of this helper.

Should we be adding new user of the helper?

/*
* XXX we want to get rid of these helpers and use the full load resolution. */
static inline long se_weight(struct sched_entity *se)

{
        return scale_load_down(se->load.weight);
}
Quoted 6 lines
kernel/sched/fair.c +0 -1
                      sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
                      if (sa->util_avg > cap)
--

2.40.1

maintainer
#5 lore raw
Quoted 7 lines · Vishal Chourasia
Quoted 4 lines · Dawei Li

Hi Vishal

Thanks for the comment!
Do you suggest using scale_load_down() in place of se_weight()?

scale_load_down should be better.

se_weight is used for computing sched_entity's pelt signal so keep using it looks better but all this clearly just nitpick because that doesn't make any difference

Quoted 79 lines
Quoted 2 lines

It's a soft bug we should fix one way or another before what the comment mentions really happens.

IIUC, We should be moving towards using full load resolution
for all the calculations. In that case, we need not worry about scaling load at all. Maybe someone could provide context here.

Quoted 72 lines

I am actually confused that we have both se_weight() and scale_load_down(), and they do the same thing.

Best regards,
Dawei

Quoted 65 lines · Vishal Chourasia
Quoted 36 lines · Dawei Li

Change se->load.weight to se_weight(se) in the calculation for the initial util_avg to avoid unnecessarily inflating the util_avg by 1024 times.

The reason is that se->load.weight has the unit/scale as the scaled-up load, while cfs_rg->avg.load_avg has the unit/scale as the true task weight (as mapped directly from the task's nice/priority value). With CONFIG_32BIT, the scaled-up load is equal to the true task weight. With CONFIG_64BIT, the scaled-up load is 1024 times the true task weight. Thus, the current code may inflate the util_avg by 1024 times. The follow-up capping will not allow the util_avg value to go wild. But the calculation should have the correct logic.

---
Changes in v2:
- update the commit message

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +2 -1
index a19ea290b790..5f98f639bdb9 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *           * se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such

kernel/sched/fair.c void post_init_entity_util_avg(struct task_struct *p) · line 1082

Quoted 5 lines
kernel/sched/fair.c +1 -1
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
      if (cap > 0) {
              if (cfs_rq->avg.util_avg != 0) {
-                     sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+                     sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);

Hi,

The comment above the declaration of se_weight function says we should be using full load resolution and get rid of this helper.

Should we be adding new user of the helper?

/*
* XXX we want to get rid of these helpers and use the full load resolution. */
static inline long se_weight(struct sched_entity *se)

{
        return scale_load_down(se->load.weight);
}
Quoted 6 lines
kernel/sched/fair.c +0 -1
                      sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
                      if (sa->util_avg > cap)
--

2.40.1

#6 lore raw
Quoted 5 lines · Vincent Guittot
Quoted 4 lines · Vishal Chourasia
Quoted 2 lines · Dawei Li

Hi Vishal

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

Thanks for the comment!
Do you suggest using scale_load_down() in place of se_weight()?

scale_load_down should be better.

se_weight is used for computing sched_entity's pelt signal so keep using it looks better but all this clearly just nitpick because that doesn't make any difference

Alright. Thank you for the clarification.

Quoted 80 lines
Quoted 80 lines
Quoted 3 lines

It's a soft bug we should fix one way or another before what the comment mentions really happens.

IIUC, We should be moving towards using full load resolution
for all the calculations. In that case, we need not worry about scaling load at all. Maybe someone could provide context here.

Quoted 72 lines

I am actually confused that we have both se_weight() and scale_load_down(), and they do the same thing.

Best regards,
Dawei

Quoted 65 lines · Vishal Chourasia
Quoted 36 lines · Dawei Li

Change se->load.weight to se_weight(se) in the calculation for the initial util_avg to avoid unnecessarily inflating the util_avg by 1024 times.

The reason is that se->load.weight has the unit/scale as the scaled-up load, while cfs_rg->avg.load_avg has the unit/scale as the true task weight (as mapped directly from the task's nice/priority value). With CONFIG_32BIT, the scaled-up load is equal to the true task weight. With CONFIG_64BIT, the scaled-up load is 1024 times the true task weight. Thus, the current code may inflate the util_avg by 1024 times. The follow-up capping will not allow the util_avg value to go wild. But the calculation should have the correct logic.

---
Changes in v2:
- update the commit message

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +2 -1
index a19ea290b790..5f98f639bdb9 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *           * se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such

kernel/sched/fair.c void post_init_entity_util_avg(struct task_struct *p) · line 1082

Quoted 5 lines
kernel/sched/fair.c +1 -1
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
      if (cap > 0) {
              if (cfs_rq->avg.util_avg != 0) {
-                     sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+                     sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);

Hi,

The comment above the declaration of se_weight function says we should be using full load resolution and get rid of this helper.

Should we be adding new user of the helper?

/*
* XXX we want to get rid of these helpers and use the full load resolution. */
static inline long se_weight(struct sched_entity *se)

{
        return scale_load_down(se->load.weight);
}
Quoted 6 lines
kernel/sched/fair.c +0 -1
                      sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
                      if (sa->util_avg > cap)
--

2.40.1

Reviewed-by on v2 1/1
#7 lore raw
In reply to Dawei Li
Quoted 13 lines · Dawei Li

Change se->load.weight to se_weight(se) in the calculation for the initial util_avg to avoid unnecessarily inflating the util_avg by 1024 times.

The reason is that se->load.weight has the unit/scale as the scaled-up load, while cfs_rg->avg.load_avg has the unit/scale as the true task weight (as mapped directly from the task's nice/priority value). With CONFIG_32BIT, the scaled-up load is equal to the true task weight. With CONFIG_64BIT, the scaled-up load is 1024 times the true task weight. Thus, the current code may inflate the util_avg by 1024 times. The follow-up capping will not allow the util_avg value to go wild. But the calculation should have the correct logic.

Quoted 1 line
Quoted 33 lines

---
Changes in v2:
- update the commit message

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +3 -2
index a19ea290b790..5f98f639bdb9 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *		* se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
 	if (cap > 0) {
 		if (cfs_rq->avg.util_avg != 0) {
-			sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+			sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);
 			sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
 			if (sa->util_avg > cap)
Signature · 1 line
2.40.1
merge bot Applied
#8 lore raw

[tip: sched/urgent] sched/fair: Fix initial util_avg calculation

In reply to Dawei Li
The following commit has been merged into the sched/urgent branch of tip:

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID:     72bffbf57c5247ac6146d1103ef42e9f8d094bc8
Gitweb:        https://git.kernel.org/tip/72bffbf57c5247ac6146d1103ef42e9f8d094bc8
Author:        Dawei Li <[email protected]>
AuthorDate:    Thu, 14 Mar 2024 18:59:16 -07:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Fri, 17 May 2024 09:49:44 +02:00

sched/fair: Fix initial util_avg calculation

Change se->load.weight to se_weight(se) in the calculation for the initial util_avg to avoid unnecessarily inflating the util_avg by 1024 times.

The reason is that se->load.weight has the unit/scale as the scaled-up load, while cfs_rg->avg.load_avg has the unit/scale as the true task weight (as mapped directly from the task's nice/priority value). With CONFIG_32BIT, the scaled-up load is equal to the true task weight. With CONFIG_64BIT, the scaled-up load is 1024 times the true task weight. Thus, the current code may inflate the util_avg by 1024 times. The follow-up capping will not allow the util_avg value to go wild. But the calculation should have the correct logic.

---
 kernel/sched/fair.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/sched/fair.c +3 -2
index 146ecf9..9009787 100644
@@ -1031,7 +1031,8 @@ void init_entity_runnable_average(struct sched_entity *se)
  * With new tasks being created, their initial util_avgs are extrapolated
  * based on the cfs_rq's current util_avg:
  *
- *   util_avg = cfs_rq->util_avg / (cfs_rq->load_avg + 1) * se.load.weight
+ *   util_avg = cfs_rq->avg.util_avg / (cfs_rq->avg.load_avg + 1)
+ *		* se_weight(se)
  *
  * However, in many cases, the above util_avg does not give a desired
  * value. Moreover, the sum of the util_avgs may be divergent, such
@@ -1078,7 +1079,7 @@ void post_init_entity_util_avg(struct task_struct *p)
 
 	if (cap > 0) {
 		if (cfs_rq->avg.util_avg != 0) {
-			sa->util_avg  = cfs_rq->avg.util_avg * se->load.weight;
+			sa->util_avg  = cfs_rq->avg.util_avg * se_weight(se);
 			sa->util_avg /= (cfs_rq->avg.load_avg + 1);
 
 			if (sa->util_avg > cap)