* [PATCH v5] sched/fair: do not scan twice in detach_tasks()
@ 2025-07-22 10:26 Huang Shijie
2025-07-22 12:31 ` Valentin Schneider
2025-07-23 7:30 ` Vincent Guittot
0 siblings, 2 replies; 3+ messages in thread
From: Huang Shijie @ 2025-07-22 10:26 UTC (permalink / raw)
To: mingo, peterz, juri.lelli, vincent.guittot, vschneid
Cc: patches, cl, Shubhang, dietmar.eggemann, rostedt, bsegall,
mgorman, linux-kernel, Huang Shijie
detach_tasks() uses struct lb_env.loop_max as an env.src_rq->cfs_tasks
iteration count limit. It is however set without the source RQ lock held.
This means that env.loop_max and the actual length of env.src_rq->cfs_tasks
as observed within detach_tasks() can differ. This can cause some tasks to
be unnecessarily iterated over more than once, for instance:
env.loop_max := 4
detach_tasks()
// Here env.src->cfs_tasks only contains two tasks which can't be
// migrated anywhere, so they're put back in the list each time.
env.src->cfs_tasks := [p1, p0]
// The iteration goes:
p0; cfs_tasks := [p0, p1]
p1; cfs_tasks := [p1, p0]
p0; cfs_tasks := [p0, p1]
p1; cfs_tasks := [p1, p0]
// IOW we iterate over each task twice
In the Specjbb test, the similar issues can be caught many times.
(Over 330,000 times in a 30-minites Specjbb test)
This patch sets env.loop_max only once RQ lock is taken,
and uses busiest->cfs.h_nr_queued for setting the env.loop_max.
After this patch, I cannot catch any above issue in the Specjbb test.
Signed-off-by: Huang Shijie <shijie@os.amperecomputing.com>
---
v4 --> v5:
Set the env.loop_max once the rq lock is taken.
v4:https://lore.kernel.org/all/20250721023939.19703-1-shijie@os.amperecomputing.com/
v3 --> v4:
Changed the commit message suggested by Valentin Schneider.
v3: https://lore.kernel.org/all/20250718063523.9232-1-shijie@os.amperecomputing.com/
v2 --> v3:
Fix a typo in the commit message.
v2: https://lore.kernel.org/all/20250718054709.8781-1-shijie@os.amperecomputing.com/
v1 --> v2:
Add more comment from Valentin Schneider
v1: https://lore.kernel.org/all/20250707083636.38380-1-shijie@os.amperecomputing.com/
---
kernel/sched/fair.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 7cc9d50e3e11..9c1f21d59b5c 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -11708,12 +11708,15 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
* still unbalanced. ld_moved simply stays zero, so it is
* correctly treated as an imbalance.
*/
- env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
-
more_balance:
rq_lock_irqsave(busiest, &rf);
update_rq_clock(busiest);
+ if (!env.loop_max)
+ env.loop_max = min(sysctl_sched_nr_migrate, busiest->cfs.h_nr_queued);
+ else
+ env.loop_max = min(env.loop_max, busiest->cfs.h_nr_queued);
+
/*
* cur_ld_moved - load moved in current iteration
* ld_moved - cumulative load moved across iterations
--
2.40.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v5] sched/fair: do not scan twice in detach_tasks()
2025-07-22 10:26 [PATCH v5] sched/fair: do not scan twice in detach_tasks() Huang Shijie
@ 2025-07-22 12:31 ` Valentin Schneider
2025-07-23 7:30 ` Vincent Guittot
1 sibling, 0 replies; 3+ messages in thread
From: Valentin Schneider @ 2025-07-22 12:31 UTC (permalink / raw)
To: Huang Shijie, mingo, peterz, juri.lelli, vincent.guittot
Cc: patches, cl, Shubhang, dietmar.eggemann, rostedt, bsegall,
mgorman, linux-kernel, Huang Shijie
On 22/07/25 18:26, Huang Shijie wrote:
> detach_tasks() uses struct lb_env.loop_max as an env.src_rq->cfs_tasks
> iteration count limit. It is however set without the source RQ lock held.
>
> This means that env.loop_max and the actual length of env.src_rq->cfs_tasks
> as observed within detach_tasks() can differ. This can cause some tasks to
> be unnecessarily iterated over more than once, for instance:
>
> env.loop_max := 4
> detach_tasks()
> // Here env.src->cfs_tasks only contains two tasks which can't be
> // migrated anywhere, so they're put back in the list each time.
> env.src->cfs_tasks := [p1, p0]
> // The iteration goes:
> p0; cfs_tasks := [p0, p1]
> p1; cfs_tasks := [p1, p0]
> p0; cfs_tasks := [p0, p1]
> p1; cfs_tasks := [p1, p0]
>
> // IOW we iterate over each task twice
>
> In the Specjbb test, the similar issues can be caught many times.
> (Over 330,000 times in a 30-minites Specjbb test)
>
> This patch sets env.loop_max only once RQ lock is taken,
> and uses busiest->cfs.h_nr_queued for setting the env.loop_max.
>
> After this patch, I cannot catch any above issue in the Specjbb test.
>
> Signed-off-by: Huang Shijie <shijie@os.amperecomputing.com>
Reviewed-by: Valentin Schneider <vschneid@redhat.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v5] sched/fair: do not scan twice in detach_tasks()
2025-07-22 10:26 [PATCH v5] sched/fair: do not scan twice in detach_tasks() Huang Shijie
2025-07-22 12:31 ` Valentin Schneider
@ 2025-07-23 7:30 ` Vincent Guittot
1 sibling, 0 replies; 3+ messages in thread
From: Vincent Guittot @ 2025-07-23 7:30 UTC (permalink / raw)
To: Huang Shijie
Cc: mingo, peterz, juri.lelli, vschneid, patches, cl, Shubhang,
dietmar.eggemann, rostedt, bsegall, mgorman, linux-kernel
On Tue, 22 Jul 2025 at 12:26, Huang Shijie
<shijie@os.amperecomputing.com> wrote:
>
> detach_tasks() uses struct lb_env.loop_max as an env.src_rq->cfs_tasks
> iteration count limit. It is however set without the source RQ lock held.
>
> This means that env.loop_max and the actual length of env.src_rq->cfs_tasks
> as observed within detach_tasks() can differ. This can cause some tasks to
> be unnecessarily iterated over more than once, for instance:
>
> env.loop_max := 4
> detach_tasks()
> // Here env.src->cfs_tasks only contains two tasks which can't be
> // migrated anywhere, so they're put back in the list each time.
> env.src->cfs_tasks := [p1, p0]
> // The iteration goes:
> p0; cfs_tasks := [p0, p1]
> p1; cfs_tasks := [p1, p0]
> p0; cfs_tasks := [p0, p1]
> p1; cfs_tasks := [p1, p0]
>
> // IOW we iterate over each task twice
>
> In the Specjbb test, the similar issues can be caught many times.
> (Over 330,000 times in a 30-minites Specjbb test)
>
> This patch sets env.loop_max only once RQ lock is taken,
> and uses busiest->cfs.h_nr_queued for setting the env.loop_max.
>
> After this patch, I cannot catch any above issue in the Specjbb test.
>
> Signed-off-by: Huang Shijie <shijie@os.amperecomputing.com>
Reviewed-by: Vincent Guittot <vincent.guittot@linaro.org>
> ---
> v4 --> v5:
> Set the env.loop_max once the rq lock is taken.
> v4:https://lore.kernel.org/all/20250721023939.19703-1-shijie@os.amperecomputing.com/
>
> v3 --> v4:
> Changed the commit message suggested by Valentin Schneider.
> v3: https://lore.kernel.org/all/20250718063523.9232-1-shijie@os.amperecomputing.com/
>
> v2 --> v3:
> Fix a typo in the commit message.
> v2: https://lore.kernel.org/all/20250718054709.8781-1-shijie@os.amperecomputing.com/
>
> v1 --> v2:
> Add more comment from Valentin Schneider
> v1: https://lore.kernel.org/all/20250707083636.38380-1-shijie@os.amperecomputing.com/
> ---
> kernel/sched/fair.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index 7cc9d50e3e11..9c1f21d59b5c 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -11708,12 +11708,15 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
> * still unbalanced. ld_moved simply stays zero, so it is
> * correctly treated as an imbalance.
> */
> - env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
> -
> more_balance:
> rq_lock_irqsave(busiest, &rf);
> update_rq_clock(busiest);
>
> + if (!env.loop_max)
> + env.loop_max = min(sysctl_sched_nr_migrate, busiest->cfs.h_nr_queued);
> + else
> + env.loop_max = min(env.loop_max, busiest->cfs.h_nr_queued);
> +
> /*
> * cur_ld_moved - load moved in current iteration
> * ld_moved - cumulative load moved across iterations
> --
> 2.40.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-07-23 7:30 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-22 10:26 [PATCH v5] sched/fair: do not scan twice in detach_tasks() Huang Shijie
2025-07-22 12:31 ` Valentin Schneider
2025-07-23 7:30 ` Vincent Guittot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).