Skip to contentSkip to conversation

sched/fair: do not scan twice in detach_tasks()

PATCH In review Huang Shijie 4 participants 4 messages Jul 22, 2025 – Feb 26 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 sched/fair: do not scan twice in detach_tasks()

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.

---
v4 --> v5:
Set the env.loop_max once the rq lock is taken. v4:https://lore.kernel.org/all/[email protected]/

v3 --> v4:
Changed the commit message suggested by Valentin Schneider.
v3: https://lore.kernel.org/all/[email protected]/

v2 --> v3:
Fix a typo in the commit message.
v2: https://lore.kernel.org/all/[email protected]/

v1 --> v2:
Add more comment from Valentin Schneider
v1: https://lore.kernel.org/all/[email protected]/

---
 kernel/sched/fair.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/sched/fair.c +5 -2
index 7cc9d50e3e11..9c1f21d59b5c 100644
@@ -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
Signature · 1 line
2.40.1
reviewer Reviewed-by on v5 1/1
#1 lore raw
Quoted 23 lines · 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)

Quoted 6 lines

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.

maintainer Reviewed-by on v5 1/1
#2 lore raw
In reply to Huang Shijie
Quoted 24 lines · 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)

Quoted 6 lines

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.

Quoted 45 lines

---
v4 --> v5:
Set the env.loop_max once the rq lock is taken. v4:https://lore.kernel.org/all/[email protected]/

v3 --> v4:
Changed the commit message suggested by Valentin Schneider.
v3: https://lore.kernel.org/all/[email protected]/

v2 --> v3:
Fix a typo in the commit message.
v2: https://lore.kernel.org/all/[email protected]/

v1 --> v2:
Add more comment from Valentin Schneider
v1: https://lore.kernel.org/all/[email protected]/

---
 kernel/sched/fair.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/sched/fair.c +5 -3
index 7cc9d50e3e11..9c1f21d59b5c 100644
@@ -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

#3 lore raw
In reply to Huang Shijie
Quoted 61 lines · 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.

---
v4 --> v5:
Set the env.loop_max once the rq lock is taken. v4:https://lore.kernel.org/all/[email protected]/

v3 --> v4:
Changed the commit message suggested by Valentin Schneider.
v3: https://lore.kernel.org/all/[email protected]/

v2 --> v3:
Fix a typo in the commit message.
v2: https://lore.kernel.org/all/[email protected]/

v1 --> v2:
Add more comment from Valentin Schneider
v1: https://lore.kernel.org/all/[email protected]/

---
 kernel/sched/fair.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/sched/fair.c +0 -2
index 7cc9d50e3e11..9c1f21d59b5c 100644
@@ -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);

kernel/sched/fair.c static int sched_balance_rq(int this_cpu, struct rq *this_rq, · line 11722

Quoted 10 lines
kernel/sched/fair.c +5 -0
 		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

Hi maintainers,

this patch seems to have gotten lost in the mailbox, are there any plans on picking it up?

Signature · 2 lines
Regards,
  Eric