[PATCH_V2] sched/fair: updates weight of cfs_rq before update_cfs_group() in enqueue_entity()

Gaowei.Pu posted 1 patch 1 year, 6 months ago
kernel/sched/fair.c | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
[PATCH_V2] sched/fair: updates weight of cfs_rq before update_cfs_group() in enqueue_entity()
Posted by Gaowei.Pu 1 year, 6 months ago
From: pugaowei <pugaowei@oppo.com>

we should update the weight of cfs_rq before update_cfs_group().
Ensure that we can get accurate shares of the cfs_rq when its
weights changes. we can find this work was done correctly in
dequeue_entity(). so fix it.

patch_V1 :
https://lore.kernel.org/lkml/20240531030833.3375-1-pugaowei@oppo.com/T/#u
trigger a warnning below because of the changing order of
account_entity_enqueue().

[ 0.400603][ T0] ? __warn (kernel/panic.c:693)

[ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5256 (discriminator 1))

[ 0.400603][ T0] ? report_bug (lib/bug.c:180 lib/bug.c:219)

[ 0.400603][ T0] ? handle_bug (arch/x86/kernel/traps.c:239)

[ 0.400603][ T0] ? exc_invalid_op (arch/x86/kernel/traps.c:260 (discriminator 1))

[ 0.400603][ T0] ? asm_exc_invalid_op (arch/x86/include/asm/idtentry.h:621)

[ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5256 (discriminator 1))

[ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5182)

[ 0.400603][ T0] enqueue_entity (kernel/sched/fair.c:5328)

[ 0.400603][ T0] enqueue_task_fair (kernel/sched/fair.c:6785)

V2 fix the warnning and keep the lag without inflating it when it is
the first sched_entity queued on the cfs_rq.

Signed-off-by: pugaowei <pugaowei@oppo.com>
---
 kernel/sched/fair.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 8a5b1ae0aa55..2fb1fbcfdda3 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -5190,12 +5190,12 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 	 *
 	 * EEVDF: placement strategy #1 / #2
 	 */
-	if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) {
+	if (sched_feat(PLACE_LAG)) {
 		struct sched_entity *curr = cfs_rq->curr;
-		unsigned long load;
+		unsigned long load, se_load;
 
 		lag = se->vlag;
-
+		se_load = scale_load_down(se->load.weight);
 		/*
 		 * If we want to place a task and preserve lag, we have to
 		 * consider the effect of the new entity on the weighted
@@ -5252,9 +5252,13 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 		if (curr && curr->on_rq)
 			load += scale_load_down(curr->load.weight);
 
-		lag *= load + scale_load_down(se->load.weight);
-		if (WARN_ON_ONCE(!load))
-			load = 1;
+		lag *= load + se_load;
+		/*
+		 * we just need to keep the lag whithout inflating it when the se is
+		 * the first sched_entity queued on cfs_rq.
+		 */
+		if (!load)
+			load = se_load;
 		lag = div_s64(lag, load);
 	}
 
@@ -5304,6 +5308,12 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 	 */
 	update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH);
 	se_update_runnable(se);
+
+	/*
+	 * we should update the weight of cfs_rq before update_cfs_group.
+	 * Ensure we can get accurate shares of the cfs_rq when its weights changes.
+	 */
+	account_entity_enqueue(cfs_rq, se);
 	/*
 	 * XXX update_load_avg() above will have attached us to the pelt sum;
 	 * but update_cfs_group() here will re-adjust the weight and have to
@@ -5318,8 +5328,6 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
 	if (!curr)
 		place_entity(cfs_rq, se, flags);
 
-	account_entity_enqueue(cfs_rq, se);
-
 	/* Entity has migrated, no longer consider this task hot */
 	if (flags & ENQUEUE_MIGRATED)
 		se->exec_start = 0;
-- 
2.17.1
Re: [PATCH_V2] sched/fair: updates weight of cfs_rq before update_cfs_group() in enqueue_entity()
Posted by Vincent Guittot 1 year, 6 months ago
On Mon, 3 Jun 2024 at 11:18, Gaowei.Pu <pugaowei@oppo.com> wrote:
>
> From: pugaowei <pugaowei@oppo.com>
>
> we should update the weight of cfs_rq before update_cfs_group().

update_cfs_group() updates the weight of se that is about to be
enqueued on cfs_rq so the current order looks good for me:

update_cfs_group()
  reweight_entity(cfs_rq, se, shares); //se->on_rq == 0
    update_load_set(&se->load, weight);
      se->load.weight = weight

account_entity_enqueue(cfs_rq, se);
  update_load_add(&cfs_rq->load, se->load.weight);
    cfs_rq->load.weight += se->load.weight

Have you faced some problems in particular ?

> Ensure that we can get accurate shares of the cfs_rq when its
> weights changes. we can find this work was done correctly in
> dequeue_entity(). so fix it.
>
> patch_V1 :
> https://lore.kernel.org/lkml/20240531030833.3375-1-pugaowei@oppo.com/T/#u
> trigger a warnning below because of the changing order of
> account_entity_enqueue().
>
> [ 0.400603][ T0] ? __warn (kernel/panic.c:693)
>
> [ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5256 (discriminator 1))
>
> [ 0.400603][ T0] ? report_bug (lib/bug.c:180 lib/bug.c:219)
>
> [ 0.400603][ T0] ? handle_bug (arch/x86/kernel/traps.c:239)
>
> [ 0.400603][ T0] ? exc_invalid_op (arch/x86/kernel/traps.c:260 (discriminator 1))
>
> [ 0.400603][ T0] ? asm_exc_invalid_op (arch/x86/include/asm/idtentry.h:621)
>
> [ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5256 (discriminator 1))
>
> [ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5182)
>
> [ 0.400603][ T0] enqueue_entity (kernel/sched/fair.c:5328)
>
> [ 0.400603][ T0] enqueue_task_fair (kernel/sched/fair.c:6785)
>
> V2 fix the warnning and keep the lag without inflating it when it is
> the first sched_entity queued on the cfs_rq.
>
> Signed-off-by: pugaowei <pugaowei@oppo.com>
> ---
>  kernel/sched/fair.c | 24 ++++++++++++++++--------
>  1 file changed, 16 insertions(+), 8 deletions(-)
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 8a5b1ae0aa55..2fb1fbcfdda3 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -5190,12 +5190,12 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>          *
>          * EEVDF: placement strategy #1 / #2
>          */
> -       if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) {
> +       if (sched_feat(PLACE_LAG)) {
>                 struct sched_entity *curr = cfs_rq->curr;
> -               unsigned long load;
> +               unsigned long load, se_load;
>
>                 lag = se->vlag;
> -
> +               se_load = scale_load_down(se->load.weight);
>                 /*
>                  * If we want to place a task and preserve lag, we have to
>                  * consider the effect of the new entity on the weighted
> @@ -5252,9 +5252,13 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>                 if (curr && curr->on_rq)
>                         load += scale_load_down(curr->load.weight);
>
> -               lag *= load + scale_load_down(se->load.weight);
> -               if (WARN_ON_ONCE(!load))
> -                       load = 1;
> +               lag *= load + se_load;
> +               /*
> +                * we just need to keep the lag whithout inflating it when the se is
> +                * the first sched_entity queued on cfs_rq.
> +                */
> +               if (!load)
> +                       load = se_load;
>                 lag = div_s64(lag, load);
>         }
>
> @@ -5304,6 +5308,12 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>          */
>         update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH);
>         se_update_runnable(se);
> +
> +       /*
> +        * we should update the weight of cfs_rq before update_cfs_group.
> +        * Ensure we can get accurate shares of the cfs_rq when its weights changes.
> +        */
> +       account_entity_enqueue(cfs_rq, se);
>         /*
>          * XXX update_load_avg() above will have attached us to the pelt sum;
>          * but update_cfs_group() here will re-adjust the weight and have to
> @@ -5318,8 +5328,6 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>         if (!curr)
>                 place_entity(cfs_rq, se, flags);
>
> -       account_entity_enqueue(cfs_rq, se);
> -
>         /* Entity has migrated, no longer consider this task hot */
>         if (flags & ENQUEUE_MIGRATED)
>                 se->exec_start = 0;
> --
> 2.17.1
>
Re: [PATCH_V2] sched/fair: updates weight of cfs_rq before update_cfs_group() in enqueue_entity()
Posted by Gaowei Pu 1 year, 6 months ago
Hi vincent,

On 2024/6/11 18:59, Vincent Guittot wrote:
> On Mon, 3 Jun 2024 at 11:18, Gaowei.Pu <pugaowei@oppo.com> wrote:
>>
>> From: pugaowei <pugaowei@oppo.com>
>>
>> we should update the weight of cfs_rq before update_cfs_group().
> 
> update_cfs_group() updates the weight of se that is about to be
> enqueued on cfs_rq so the current order looks good for me:
> 
> update_cfs_group()
>   reweight_entity(cfs_rq, se, shares); //se->on_rq == 0
>     update_load_set(&se->load, weight);
>       se->load.weight = weight
> 
> account_entity_enqueue(cfs_rq, se);
>   update_load_add(&cfs_rq->load, se->load.weight);
>     cfs_rq->load.weight += se->load.weight
> 
> Have you faced some problems in particular ?
sorry, made a mistask.
account_entity_enqueue(cfs_rq, se) updates weight of cfs_rq on which entity queued.
update_cfs_group(se) updates weight of the cfs_rq owned by the entity. so there is no conflict
or running order problem.
thanks for your reply~
> 
>> Ensure that we can get accurate shares of the cfs_rq when its
>> weights changes. we can find this work was done correctly in
>> dequeue_entity(). so fix it.
>>
>> patch_V1 :
>> https://lore.kernel.org/lkml/20240531030833.3375-1-pugaowei@oppo.com/T/#u
>> trigger a warnning below because of the changing order of
>> account_entity_enqueue().
>>
>> [ 0.400603][ T0] ? __warn (kernel/panic.c:693)
>>
>> [ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5256 (discriminator 1))
>>
>> [ 0.400603][ T0] ? report_bug (lib/bug.c:180 lib/bug.c:219)
>>
>> [ 0.400603][ T0] ? handle_bug (arch/x86/kernel/traps.c:239)
>>
>> [ 0.400603][ T0] ? exc_invalid_op (arch/x86/kernel/traps.c:260 (discriminator 1))
>>
>> [ 0.400603][ T0] ? asm_exc_invalid_op (arch/x86/include/asm/idtentry.h:621)
>>
>> [ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5256 (discriminator 1))
>>
>> [ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5182)
>>
>> [ 0.400603][ T0] enqueue_entity (kernel/sched/fair.c:5328)
>>
>> [ 0.400603][ T0] enqueue_task_fair (kernel/sched/fair.c:6785)
>>
>> V2 fix the warnning and keep the lag without inflating it when it is
>> the first sched_entity queued on the cfs_rq.
>>
>> Signed-off-by: pugaowei <pugaowei@oppo.com>
>> ---
>>  kernel/sched/fair.c | 24 ++++++++++++++++--------
>>  1 file changed, 16 insertions(+), 8 deletions(-)
>>
>> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
>> index 8a5b1ae0aa55..2fb1fbcfdda3 100644
>> --- a/kernel/sched/fair.c
>> +++ b/kernel/sched/fair.c
>> @@ -5190,12 +5190,12 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>>          *
>>          * EEVDF: placement strategy #1 / #2
>>          */
>> -       if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) {
>> +       if (sched_feat(PLACE_LAG)) {
>>                 struct sched_entity *curr = cfs_rq->curr;
>> -               unsigned long load;
>> +               unsigned long load, se_load;
>>
>>                 lag = se->vlag;
>> -
>> +               se_load = scale_load_down(se->load.weight);
>>                 /*
>>                  * If we want to place a task and preserve lag, we have to
>>                  * consider the effect of the new entity on the weighted
>> @@ -5252,9 +5252,13 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>>                 if (curr && curr->on_rq)
>>                         load += scale_load_down(curr->load.weight);
>>
>> -               lag *= load + scale_load_down(se->load.weight);
>> -               if (WARN_ON_ONCE(!load))
>> -                       load = 1;
>> +               lag *= load + se_load;
>> +               /*
>> +                * we just need to keep the lag whithout inflating it when the se is
>> +                * the first sched_entity queued on cfs_rq.
>> +                */
>> +               if (!load)
>> +                       load = se_load;
>>                 lag = div_s64(lag, load);
>>         }
>>
>> @@ -5304,6 +5308,12 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>>          */
>>         update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH);
>>         se_update_runnable(se);
>> +
>> +       /*
>> +        * we should update the weight of cfs_rq before update_cfs_group.
>> +        * Ensure we can get accurate shares of the cfs_rq when its weights changes.
>> +        */
>> +       account_entity_enqueue(cfs_rq, se);
>>         /*
>>          * XXX update_load_avg() above will have attached us to the pelt sum;
>>          * but update_cfs_group() here will re-adjust the weight and have to
>> @@ -5318,8 +5328,6 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>>         if (!curr)
>>                 place_entity(cfs_rq, se, flags);
>>
>> -       account_entity_enqueue(cfs_rq, se);
>> -
>>         /* Entity has migrated, no longer consider this task hot */
>>         if (flags & ENQUEUE_MIGRATED)
>>                 se->exec_start = 0;
>> --
>> 2.17.1
>>
Re: [PATCH_V2] sched/fair: updates weight of cfs_rq before update_cfs_group() in enqueue_entity()
Posted by Gaowei Pu 1 year, 6 months ago
ping...

On 2024/6/3 17:18, Gaowei.Pu wrote:
> From: pugaowei <pugaowei@oppo.com>
> 
> we should update the weight of cfs_rq before update_cfs_group().
> Ensure that we can get accurate shares of the cfs_rq when its
> weights changes. we can find this work was done correctly in
> dequeue_entity(). so fix it.
> 
> patch_V1 :
> https://lore.kernel.org/lkml/20240531030833.3375-1-pugaowei@oppo.com/T/#u
> trigger a warnning below because of the changing order of
> account_entity_enqueue().
> 
> [ 0.400603][ T0] ? __warn (kernel/panic.c:693)
> 
> [ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5256 (discriminator 1))
> 
> [ 0.400603][ T0] ? report_bug (lib/bug.c:180 lib/bug.c:219)
> 
> [ 0.400603][ T0] ? handle_bug (arch/x86/kernel/traps.c:239)
> 
> [ 0.400603][ T0] ? exc_invalid_op (arch/x86/kernel/traps.c:260 (discriminator 1))
> 
> [ 0.400603][ T0] ? asm_exc_invalid_op (arch/x86/include/asm/idtentry.h:621)
> 
> [ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5256 (discriminator 1))
> 
> [ 0.400603][ T0] ? place_entity (kernel/sched/fair.c:5182)
> 
> [ 0.400603][ T0] enqueue_entity (kernel/sched/fair.c:5328)
> 
> [ 0.400603][ T0] enqueue_task_fair (kernel/sched/fair.c:6785)
> 
> V2 fix the warnning and keep the lag without inflating it when it is
> the first sched_entity queued on the cfs_rq.
> 
> Signed-off-by: pugaowei <pugaowei@oppo.com>
> ---
>  kernel/sched/fair.c | 24 ++++++++++++++++--------
>  1 file changed, 16 insertions(+), 8 deletions(-)
> 
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 8a5b1ae0aa55..2fb1fbcfdda3 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -5190,12 +5190,12 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>  	 *
>  	 * EEVDF: placement strategy #1 / #2
>  	 */
> -	if (sched_feat(PLACE_LAG) && cfs_rq->nr_running) {
> +	if (sched_feat(PLACE_LAG)) {
>  		struct sched_entity *curr = cfs_rq->curr;
> -		unsigned long load;
> +		unsigned long load, se_load;
>  
>  		lag = se->vlag;
> -
> +		se_load = scale_load_down(se->load.weight);
>  		/*
>  		 * If we want to place a task and preserve lag, we have to
>  		 * consider the effect of the new entity on the weighted
> @@ -5252,9 +5252,13 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>  		if (curr && curr->on_rq)
>  			load += scale_load_down(curr->load.weight);
>  
> -		lag *= load + scale_load_down(se->load.weight);
> -		if (WARN_ON_ONCE(!load))
> -			load = 1;
> +		lag *= load + se_load;
> +		/*
> +		 * we just need to keep the lag whithout inflating it when the se is
> +		 * the first sched_entity queued on cfs_rq.
> +		 */
> +		if (!load)
> +			load = se_load;
>  		lag = div_s64(lag, load);
>  	}
>  
> @@ -5304,6 +5308,12 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>  	 */
>  	update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH);
>  	se_update_runnable(se);
> +
> +	/*
> +	 * we should update the weight of cfs_rq before update_cfs_group.
> +	 * Ensure we can get accurate shares of the cfs_rq when its weights changes.
> +	 */
> +	account_entity_enqueue(cfs_rq, se);
>  	/*
>  	 * XXX update_load_avg() above will have attached us to the pelt sum;
>  	 * but update_cfs_group() here will re-adjust the weight and have to
> @@ -5318,8 +5328,6 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
>  	if (!curr)
>  		place_entity(cfs_rq, se, flags);
>  
> -	account_entity_enqueue(cfs_rq, se);
> -
>  	/* Entity has migrated, no longer consider this task hot */
>  	if (flags & ENQUEUE_MIGRATED)
>  		se->exec_start = 0;