2024-03-25 17:10:09

by Shrikanth Hegde

[permalink] [raw]
Subject: [PATCH] sched/fair: Simplify continue_balancing for newidle

newidle(CPU_NEWLY_IDLE) balancing doesn't stop the load balancing if the
continue_balancing flag is reset. Other two balancing (IDLE, BUSY) do
that. newidle balance stops the load balancing if rq has a task or there
is wakeup pending. The same checks are present in should_we_balance for
newidle. Hence use the return value and simplify continue_balancing
mechanism for newidle. Update the comment surrounding it as well.

No change in functionality intended.

Signed-off-by: Shrikanth Hegde <[email protected]>
---
kernel/sched/fair.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index f00cb66cc479..d80535df8f03 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -12307,6 +12307,7 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
{
unsigned long next_balance = jiffies + HZ;
int this_cpu = this_rq->cpu;
+ int continue_balancing = 1;
u64 t0, t1, curr_cost = 0;
struct sched_domain *sd;
int pulled_task = 0;
@@ -12321,8 +12322,9 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
return 0;

/*
- * We must set idle_stamp _before_ calling idle_balance(), such that we
- * measure the duration of idle_balance() as idle time.
+ * We must set idle_stamp _before_ calling sched_balance_rq()
+ * for CPU_NEWLY_IDLE, such that we measure the this duration
+ * as idle time.
*/
this_rq->idle_stamp = rq_clock(this_rq);

@@ -12361,7 +12363,6 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)

rcu_read_lock();
for_each_domain(this_cpu, sd) {
- int continue_balancing = 1;
u64 domain_cost;

update_next_balance(sd, &next_balance);
@@ -12387,8 +12388,7 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
* Stop searching for tasks to pull if there are
* now runnable tasks on this rq.
*/
- if (pulled_task || this_rq->nr_running > 0 ||
- this_rq->ttwu_pending)
+ if (pulled_task || !continue_balancing)
break;
}
rcu_read_unlock();
--
2.39.3



2024-03-26 08:09:33

by Ingo Molnar

[permalink] [raw]
Subject: Re: [PATCH] sched/fair: Simplify continue_balancing for newidle


* Shrikanth Hegde <[email protected]> wrote:

> newidle(CPU_NEWLY_IDLE) balancing doesn't stop the load balancing if the
> continue_balancing flag is reset. Other two balancing (IDLE, BUSY) do
> that. newidle balance stops the load balancing if rq has a task or there
> is wakeup pending. The same checks are present in should_we_balance for
> newidle. Hence use the return value and simplify continue_balancing
> mechanism for newidle. Update the comment surrounding it as well.

Assuming there are no side-effects to balancing behavior.

> No change in functionality intended.

Is this actually true? Any change to behavior invalidates such a sentence.

> /*
> + * We must set idle_stamp _before_ calling sched_balance_rq()
> + * for CPU_NEWLY_IDLE, such that we measure the this duration
> + * as idle time.
> */

'the this' ...?

Thanks,

Ingo

2024-03-26 09:01:32

by Shrikanth Hegde

[permalink] [raw]
Subject: Re: [PATCH] sched/fair: Simplify continue_balancing for newidle



On 3/26/24 1:37 PM, Ingo Molnar wrote:
>
> * Shrikanth Hegde <[email protected]> wrote:
>
>> newidle(CPU_NEWLY_IDLE) balancing doesn't stop the load balancing if the
>> continue_balancing flag is reset. Other two balancing (IDLE, BUSY) do
>> that. newidle balance stops the load balancing if rq has a task or there
>> is wakeup pending. The same checks are present in should_we_balance for
>> newidle. Hence use the return value and simplify continue_balancing
>> mechanism for newidle. Update the comment surrounding it as well.
>
> Assuming there are no side-effects to balancing behavior.

I ran hackbench. More or less same results with patch. But thats very
limited set of benchmarks. Let me do some more testing with it and send the
results.

>
>> No change in functionality intended.
>
> Is this actually true? Any change to behavior invalidates such a sentence.

From what i think, code path is same and I don't see any functionality changing.
Correct me if i am wrong.

Currently, sched_balance_newidle does the same check to bail out as the
should_we_balance check in case of newidle. i.e

should_we_balance
if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending)
return 0;

sched_balance_newidle
if (pulled_task || this_rq->nr_running > 0 ||
this_rq->ttwu_pending)
break;
}

>
>> /*
>> + * We must set idle_stamp _before_ calling sched_balance_rq()
>> + * for CPU_NEWLY_IDLE, such that we measure the this duration
>> + * as idle time.
>> */
>
> 'the this' ...?

Sorry for the typo. it should be.
"such that we measure this duration as idle time"

>
> Thanks,
>
> Ingo

2024-03-26 15:11:55

by Dietmar Eggemann

[permalink] [raw]
Subject: Re: [PATCH] sched/fair: Simplify continue_balancing for newidle

On 26/03/2024 10:00, Shrikanth Hegde wrote:
>
> On 3/26/24 1:37 PM, Ingo Molnar wrote:
>>
>> * Shrikanth Hegde <[email protected]> wrote:
>>

[...]

>> Is this actually true? Any change to behavior invalidates such a sentence.
>
> From what i think, code path is same and I don't see any functionality changing.
> Correct me if i am wrong.
>
> Currently, sched_balance_newidle does the same check to bail out as the
> should_we_balance check in case of newidle. i.e
>
> should_we_balance
> if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending)
> return 0;
>
> sched_balance_newidle
> if (pulled_task || this_rq->nr_running > 0 ||
> this_rq->ttwu_pending)
> break;
> }

LGTM. Commit 792b9f65a568 ("sched: Allow newidle balancing to bail out
of load_balance") (Jun 22) made sure that we leave sched_balance_rq()
(former load_balance()) for CPU_NEWLY_IDLE asap to reduce wakeup latency.

So IMHO, we can use 'continue_balancing' instead of 'this_rq->nr_running
> 0 || this_rq->ttwu_pending' in sched_balance_newidle() (former
newidle_balance()).

Reviewed-by: Dietmar Eggemann <[email protected]>

2024-03-26 19:16:16

by Ingo Molnar

[permalink] [raw]
Subject: Re: [PATCH] sched/fair: Simplify continue_balancing for newidle


* Dietmar Eggemann <[email protected]> wrote:

> On 26/03/2024 10:00, Shrikanth Hegde wrote:
> >
> > On 3/26/24 1:37 PM, Ingo Molnar wrote:
> >>
> >> * Shrikanth Hegde <[email protected]> wrote:
> >>
>
> [...]
>
> >> Is this actually true? Any change to behavior invalidates such a sentence.
> >
> > From what i think, code path is same and I don't see any functionality changing.
> > Correct me if i am wrong.
> >
> > Currently, sched_balance_newidle does the same check to bail out as the
> > should_we_balance check in case of newidle. i.e
> >
> > should_we_balance
> > if (env->dst_rq->nr_running > 0 || env->dst_rq->ttwu_pending)
> > return 0;
> >
> > sched_balance_newidle
> > if (pulled_task || this_rq->nr_running > 0 ||
> > this_rq->ttwu_pending)
> > break;
> > }
>
> LGTM. Commit 792b9f65a568 ("sched: Allow newidle balancing to bail out
> of load_balance") (Jun 22) made sure that we leave sched_balance_rq()
> (former load_balance()) for CPU_NEWLY_IDLE asap to reduce wakeup latency.
>
> So IMHO, we can use 'continue_balancing' instead of 'this_rq->nr_running
> > 0 || this_rq->ttwu_pending' in sched_balance_newidle() (former
> newidle_balance()).
>
> Reviewed-by: Dietmar Eggemann <[email protected]>

Thanks for the clarification, applied!

Ingo

2024-03-26 19:24:54

by tip-bot2 for Tony Luck

[permalink] [raw]
Subject: [tip: sched/core] sched/fair: Simplify the continue_balancing logic in sched_balance_newidle()

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

Commit-ID: c829d6818b60c591f70c060b2bb75d76cf0cec6d
Gitweb: https://git.kernel.org/tip/c829d6818b60c591f70c060b2bb75d76cf0cec6d
Author: Shrikanth Hegde <[email protected]>
AuthorDate: Mon, 25 Mar 2024 21:09:26 +05:30
Committer: Ingo Molnar <[email protected]>
CommitterDate: Tue, 26 Mar 2024 20:16:20 +01:00

sched/fair: Simplify the continue_balancing logic in sched_balance_newidle()

newidle(CPU_NEWLY_IDLE) balancing doesn't stop the load-balancing if the
continue_balancing flag is reset, but the other two balancing (IDLE, BUSY)
cases do that.

newidle balance stops the load balancing if rq has a task or there
is wakeup pending. The same checks are present in should_we_balance for
newidle. Hence use the return value and simplify continue_balancing
mechanism for newidle. Update the comment surrounding it as well.

No change in functionality intended.

Signed-off-by: Shrikanth Hegde <[email protected]>
Signed-off-by: Ingo Molnar <[email protected]>
Reviewed-by: Dietmar Eggemann <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
---
kernel/sched/fair.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 24a7530..1856e58 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -12358,6 +12358,7 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
{
unsigned long next_balance = jiffies + HZ;
int this_cpu = this_rq->cpu;
+ int continue_balancing = 1;
u64 t0, t1, curr_cost = 0;
struct sched_domain *sd;
int pulled_task = 0;
@@ -12372,8 +12373,9 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
return 0;

/*
- * We must set idle_stamp _before_ calling idle_balance(), such that we
- * measure the duration of idle_balance() as idle time.
+ * We must set idle_stamp _before_ calling sched_balance_rq()
+ * for CPU_NEWLY_IDLE, such that we measure the this duration
+ * as idle time.
*/
this_rq->idle_stamp = rq_clock(this_rq);

@@ -12412,7 +12414,6 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)

rcu_read_lock();
for_each_domain(this_cpu, sd) {
- int continue_balancing = 1;
u64 domain_cost;

update_next_balance(sd, &next_balance);
@@ -12438,8 +12439,7 @@ static int sched_balance_newidle(struct rq *this_rq, struct rq_flags *rf)
* Stop searching for tasks to pull if there are
* now runnable tasks on this rq.
*/
- if (pulled_task || this_rq->nr_running > 0 ||
- this_rq->ttwu_pending)
+ if (pulled_task || !continue_balancing)
break;
}
rcu_read_unlock();