sched/pelt: Refine the enqueue_load_avg calculate method
Subsystems (2)
PATCH v2 1/1 sched/pelt: Refine the enqueue_load_avg calculate method
From: kuyo chang <[email protected]>
I meet the warning message at cfs_rq_is_decayed at below code.
SCHED_WARN_ON(cfs_rq->avg.load_avg ||
cfs_rq->avg.util_avg || cfs_rq->avg.runnable_avg)
Following is the calltrace.
Call trace:
__update_blocked_fairupdate_blocked_averagesnewidle_balancepick_next_task_fair__schedule
schedulepipe_readvfs_readksys_read
After code analyzing and some debug messages, I found it exits a corner case at attach_entity_load_avg which will cause load_sum is null but load_avg is not.
Consider se_weight is 88761 according by sched_prio_to_weight table. And assume the get_pelt_divider() is 47742, se->avg.load_avg is 1. By the calculating for se->avg.load_sum as following will become zero as following.se->avg.load_sum =
div_u64(se->avg.load_avg * se->avg.load_sum, se_weight(se));
se->avg.load_sum = 1*47742/88761 = 0.
After enqueue_load_avg code as below. cfs_rq->avg.load_avg += se->avg.load_avg; cfs_rq->avg.load_sum += se_weight(se) * se->avg.load_sum;
Then the load_sum for cfs_rq will be 1 while the load_sum for cfs_rq is 0. So it will hit the warning message.
In order to fix the corner case, make sure the se->load_avg|sum is correct before enqueue_load_avg.
After long time testing, the kernel warning was gone and the system runs as well as before.
---
v1->v2:
(1)Thanks for suggestion from Peter Zijlstra & Vincent Guittot. (2)By suggestion from Vincent Guittot,
rework the se->load_sum calculation method for fix the corner case, make sure the se->load_avg|sum is correct before enqueue_load_avg. (3)Rework changlog.
kernel/sched/fair.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
kernel/sched/fair.c +5 -3
@@ -3829,10 +3829,12 @@ static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s se->avg.runnable_sum = se->avg.runnable_avg * divider; - se->avg.load_sum = divider; -if (se_weight (se)) { + se->avg.load_sum = se->avg.load_avg * divider; +if (se_weight (se) < se->avg.load_sum) { se->avg.load_sum = -div_u64 (se->avg.load_avg * se->avg.load_sum,se_weight (se)); +div_u64 (se->avg.load_sum,se_weight (se)); + }else { + se->avg.load_sum =1 ; }enqueue_load_avg (cfs_rq, se);
Signature · 1 line
2.18.0
Quoted 37 lines · Kuyo Chang
From: kuyo chang <[email protected]>
I meet the warning message at
cfs_rq_is_decayedat below code.SCHED_WARN_ON(cfs_rq->avg.load_avg ||
cfs_rq->avg.util_avg || cfs_rq->avg.runnable_avg)Following is the calltrace.
Call trace:
__update_blocked_fairupdate_blocked_averagesnewidle_balancepick_next_task_fair__schedule
schedulepipe_readvfs_readksys_readAfter code analyzing and some debug messages, I found it exits a corner case at
attach_entity_load_avgwhich will causeload_sumis null butload_avgis not.
Considerse_weightis 88761 according bysched_prio_to_weighttable. And assume theget_pelt_divider()is 47742,se->avg.load_avgis 1. By the calculating forse->avg.load_sumas following will become zero as following.se->avg.load_sum=div_u64(se->avg.load_avg * se->avg.load_sum, se_weight(se));
se->avg.load_sum= 1*47742/88761 = 0.After
enqueue_load_avgcode as below.cfs_rq->avg.load_avg+=se->avg.load_avg;cfs_rq->avg.load_sum+=se_weight(se)*se->avg.load_sum;
Quoted 8 lines
Then the
load_sumforcfs_rqwill be 1 while theload_sumforcfs_rqis 0. So it will hit the warning message.In order to fix the corner case, make sure the se->load_avg|sum is correct before
enqueue_load_avg.After long time testing, the kernel warning was gone and the system runs as well as before.
This needs a fix tag:
Quoted 2 lines
Signed-off-by kuyo chang <[email protected]>
Quoted 36 lines
---
v1->v2:(1)Thanks for suggestion from Peter Zijlstra & Vincent Guittot. (2)By suggestion from Vincent Guittot,
rework these->load_sumcalculation method for fix the corner case, make sure the se->load_avg|sum is correct beforeenqueue_load_avg. (3)Rework changlog.kernel/sched/fair.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)kernel/sched/fair.c +5
-4@@ -3829,10 +3829,12 @@ static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s se->avg.runnable_sum = se->avg.runnable_avg * divider; - se->avg.load_sum = divider; -if (se_weight (se)) { + se->avg.load_sum = se->avg.load_avg * divider; +if (se_weight (se) < se->avg.load_sum) { se->avg.load_sum = -div_u64 (se->avg.load_avg * se->avg.load_sum,se_weight (se)); +div_u64 (se->avg.load_sum,se_weight (se)); + }else { + se->avg.load_sum =1 ; }enqueue_load_avg (cfs_rq, se); --2.18.0
Quoted 52 lines · Vincent Guittot
Quoted 50 lines · Kuyo Chang
From: kuyo chang <[email protected]>
I meet the warning message at
cfs_rq_is_decayedat below code.SCHED_WARN_ON(cfs_rq->avg.load_avg ||
cfs_rq->avg.util_avg || cfs_rq->avg.runnable_avg)Following is the calltrace.
Call trace:
__update_blocked_fairupdate_blocked_averagesnewidle_balancepick_next_task_fair__schedule
schedulepipe_readvfs_readksys_readAfter code analyzing and some debug messages, I found it exits a corner
case atattach_entity_load_avgwhich will causeload_sumis null butload_avgis not.
Considerse_weightis 88761 according bysched_prio_to_weighttable.
And assume theget_pelt_divider()is 47742,se->avg.load_avgis 1. By the calculating forse->avg.load_sumas following will become zero
as following.se->avg.load_sum=div_u64(se->avg.load_avg * se->avg.load_sum,
se_weight(se));se->avg.load_sum= 1*47742/88761 = 0.After
enqueue_load_avgcode as below.cfs_rq->avg.load_avg+=se->avg.load_avg;cfs_rq->avg.load_sum+=se_weight(se)*se->avg.load_sum;Then the
load_sumforcfs_rqwill be 1 while theload_sumforcfs_rqis 0.
So it will hit the warning message.In order to fix the corner case, make sure the se->load_avg|sum is correct
beforeenqueue_load_avg.
Quoted 7 lines
Quoted 3 lines
After long time testing, the kernel warning was gone and the system runs
as well as before.This needs a fix tag:
Fixes f207934fb79d ("sched/fair: Align PELT windows between cfs_rqand its se")
Thanks for your friendly reminder.
Quoted 43 lines
Quoted 2 lines
Signed-off-by kuyo chang <[email protected]>Reviewed-by Vincent Guittot <[email protected]>Quoted 38 lines
---
v1->v2:(1)Thanks for suggestion from Peter Zijlstra & Vincent Guittot. (2)By suggestion from Vincent Guittot,
rework these->load_sumcalculation method for fix the corner case, make sure the se->load_avg|sum is correct beforeenqueue_load_avg. (3)Rework changlog.kernel/sched/fair.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)kernel/sched/fair.c +0
-0@@ -3829,10 +3829,12 @@ static void attach_entity_load_avg(struct
cfs_rq*cfs_rq, structsched_entity*sse->avg.runnable_sum = se->avg.runnable_avg * divider;kernel/sched/fair.c +2
-3- se->avg.load_sum = divider; -if (se_weight (se)) { + se->avg.load_sum = se->avg.load_avg * divider; +if (se_weight (se) < se->avg.load_sum) { se->avg.load_sum = - div_u64(se->avg.load_avg * se-Quoted 1 line
avg.load_sum,
se_weight(se));kernel/sched/fair.c +3
-1+div_u64 (se->avg.load_sum,se_weight (se)); + }else { + se->avg.load_sum =1 ; }enqueue_load_avg (cfs_rq, se); --2.18.0
Quoted 1 line · Kuyo Chang
From: kuyo chang <[email protected]>
[...]
Quoted 5 lines
kernel/sched/fair.c +0
-0
kernel/sched/fair.c static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s · line 3835
Quoted 10 lines
kernel/sched/fair.c +3
-3@@ -3829,10 +3829,12 @@ static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s se->avg.runnable_sum = se->avg.runnable_avg * divider; - se->avg.load_sum = divider; -if (se_weight (se)) { + se->avg.load_sum = se->avg.load_avg * divider; +if (se_weight (se) < se->avg.load_sum) { se->avg.load_sum = -div_u64 (se->avg.load_avg * se->avg.load_sum,se_weight (se)); +div_u64 (se->avg.load_sum,se_weight (se));
Seems that this will fit on one line now. No braces needed then.
kernel/sched/fair.c static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s · line 3840
Quoted 5 lines
kernel/sched/fair.c +2
-0+ }else { + se->avg.load_sum =1 ; }enqueue_load_avg (cfs_rq, se);
Looks like taskgroups are not affected since they get always online with cpu.shares/weight = 1024 (cgroup v1):
cpu_cgroup_css_online() -> online_fair_sched_group() -> attach_entity_cfs_rq() -> attach_entity_load_avg()
And reweight_entity() does not have this issue.
Tested with qemu-system-x86_64 ... cores=64 ... -enable-kvm and weight=88761 for nice=0 tasks plus forcing se->avg.load_avg = 1 before the div_u64() in attach_entity_load_avg().
Quoted 14 lines · Dietmar Eggemann
Quoted 1 line · Kuyo Chang
From: kuyo chang <[email protected]>
[...]
Quoted 9 lines
kernel/sched/fair.c +0
-0@@ -3829,10 +3829,12 @@ static void attach_entity_load_avg(struct
cfs_rq*cfs_rq, structsched_entity*s
se->avg.runnable_sum=se->avg.runnable_avg* divider;
Quoted 10 lines
kernel/sched/fair.c static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s · line 3835
Quoted 8 lines
kernel/sched/fair.c +2
-3- se->avg.load_sum = divider; -if (se_weight (se)) { + se->avg.load_sum = se->avg.load_avg * divider; +if (se_weight (se) < se->avg.load_sum) { se->avg.load_sum = - div_u64(se->avg.load_avg * se->avg.load_sum,
se_weight(se));kernel/sched/fair.c +1
-0+div_u64 (se->avg.load_sum,se_weight (se));Seems that this will fit on one line now. No braces needed then.
Thanks for your friendly reminder.
Quoted 21 lines
kernel/sched/fair.c static void attach_entity_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *s · line 3840
Quoted 6 lines
kernel/sched/fair.c +2
-0+ }else { + se->avg.load_sum =1 ; }enqueue_load_avg (cfs_rq, se);Looks like taskgroups are not affected since they get always online with cpu.shares/weight = 1024 (cgroup v1):
cpu_cgroup_css_online()->online_fair_sched_group()->attach_entity_cfs_rq()->attach_entity_load_avg()And
reweight_entity()does not have this issue.Tested with
qemu-system-x86_64 ... cores=64 ... -enable-kvmand weight=88761 for nice=0 tasks plus forcingse->avg.load_avg= 1 before
thediv_u64()inattach_entity_load_avg().Tested-by Dietmar Eggemann <[email protected]>