Skip to contentSkip to conversation

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

PATCH Superseded Huang Shijie 4 participants 9 messages Jul 21, 2025 – Jul 22, 2025 Scheduler

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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, and besides detach_tasks() can be re-invoked after releasing and re-acquiring the RQ lock per LBF_NEED_BREAK.

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)

The patch introduces "first_back" to record the first task which is put back to the task list. If we get a task which is equal to first_back, we break the loop, and avoid to scan twice for it.

---
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 | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/sched/fair.c +9 -0
index 7e2963efe800..7cc9d50e3e11 100644
@@ -9443,6 +9443,7 @@ static int detach_tasks(struct lb_env *env)
 {
 	struct list_head *tasks = &env->src_rq->cfs_tasks;
 	unsigned long util, load;
+	struct task_struct *first_back = NULL;
 	struct task_struct *p;
 	int detached = 0;
 
@@ -9481,6 +9482,12 @@ static int detach_tasks(struct lb_env *env)
 		}
 
 		p = list_last_entry(tasks, struct task_struct, se.group_node);
+		/*
+		 * We're back to an already visited task that couldn't be
+		 * detached, we've seen all there is to see.
+		 */
+		if (p == first_back)
+			break;
 
 		if (!can_migrate_task(p, env))
 			goto next;
@@ -9562,6 +9569,8 @@ static int detach_tasks(struct lb_env *env)
 			schedstat_inc(p->stats.nr_failed_migrations_hot);
 
 		list_move(&p->se.group_node, tasks);
+		if (!first_back)
+			first_back = p;
 	}
 
 	/*
Signature · 1 line
2.40.1
maintainer
#1 lore raw
Quoted 8 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, and besides detach_tasks() can be re-invoked after releasing and re-acquiring the RQ lock per LBF_NEED_BREAK.

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

why not setting env.loop_max only once rq lock is taken in this case ?

side note : by default loop_max <= loop_break

Quoted 77 lines

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)

The patch introduces "first_back" to record the first task which is put back to the task list. If we get a task which is equal to first_back, we break the loop, and avoid to scan twice for it.

---
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 | 9 +++++++++
 1 file changed, 9 insertions(+)
kernel/sched/fair.c +9 -1
index 7e2963efe800..7cc9d50e3e11 100644
@@ -9443,6 +9443,7 @@ static int detach_tasks(struct lb_env *env)
 {
        struct list_head *tasks = &env->src_rq->cfs_tasks;
        unsigned long util, load;
+       struct task_struct *first_back = NULL;
        struct task_struct *p;
        int detached = 0;
 
@@ -9481,6 +9482,12 @@ static int detach_tasks(struct lb_env *env)
                }
 
                p = list_last_entry(tasks, struct task_struct, se.group_node);
+               /*
+                * We're back to an already visited task that couldn't be
+                * detached, we've seen all there is to see.
+                */
+               if (p == first_back)
+                       break;
 
                if (!can_migrate_task(p, env))
                        goto next;
@@ -9562,6 +9569,8 @@ static int detach_tasks(struct lb_env *env)
                        schedstat_inc(p->stats.nr_failed_migrations_hot);
 
                list_move(&p->se.group_node, tasks);
+               if (!first_back)
+                       first_back = p;
        }
 
        /*
--

2.40.1

#2 lore raw
Quoted 8 lines · Vincent Guittot
Quoted 6 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, and besides detach_tasks() can be re-invoked after releasing and re-acquiring the RQ lock per LBF_NEED_BREAK.

Quoted 7 lines
Quoted 2 lines

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

why not setting env.loop_max only once rq lock is taken in this case ?

side note : by default loop_max <= loop_break

I thought so too and dismissed that due to LBF_NEED_BREAK, but I guess we could still do something like:

---

kernel/sched/fair.c +5 -3
index b9b4bbbf0af6f..eef3a0d341661 100644
@@ -11643,6 +11643,7 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
 		.dst_grpmask    = group_balance_mask(sd->groups),
 		.idle		= idle,
 		.loop_break	= SCHED_NR_MIGRATE_BREAK,
+		.loop_max       = UINT_MAX,
 		.cpus		= cpus,
 		.fbq_type	= all,
 		.tasks		= LIST_HEAD_INIT(env.tasks),
@@ -11681,18 +11682,19 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
 	/* Clear this flag as soon as we find a pullable task */
 	env.flags |= LBF_ALL_PINNED;
 	if (busiest->nr_running > 1) {
+more_balance:
 		/*
 		 * Attempt to move tasks. If sched_balance_find_src_group has found
 		 * an imbalance but busiest->nr_running <= 1, the group is
 		 * 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);
 
+
+		env.loop_max = min3(env.loop_max, sysctl_sched_nr_migrate, busiest->h_nr_running);
+
 		/*
 		 * cur_ld_moved - load moved in current iteration
 		 * ld_moved     - cumulative load moved across iterations
#3 lore raw
Quoted 39 lines · Valentin Schneider
Quoted 7 lines · Vincent Guittot
Quoted 5 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, and besides detach_tasks() can be re-invoked after releasing and re-acquiring the RQ lock per LBF_NEED_BREAK.

Quoted 6 lines
Quoted 2 lines

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

why not setting env.loop_max only once rq lock is taken in this case ?

side note : by default loop_max <= loop_break

I thought so too and dismissed that due to LBF_NEED_BREAK, but I guess we could still do something like:

---

kernel/sched/fair.c +2 -0
index b9b4bbbf0af6f..eef3a0d341661 100644
@@ -11643,6 +11643,7 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
  		.dst_grpmask    = group_balance_mask(sd->groups),
  		.idle		= idle,
  		.loop_break	= SCHED_NR_MIGRATE_BREAK,
+		.loop_max       = UINT_MAX,
  		.cpus		= cpus,
  		.fbq_type	= all,
  		.tasks		= LIST_HEAD_INIT(env.tasks),
@@ -11681,18 +11682,19 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
  	/* Clear this flag as soon as we find a pullable task */
  	env.flags |= LBF_ALL_PINNED;
  	if (busiest->nr_running > 1) {
+more_balance:
  		/*
  		 * Attempt to move tasks. If sched_balance_find_src_group has found
  		 * an imbalance but busiest->nr_running <= 1, the group is
  		 * still unbalanced. ld_moved simply stays zero, so it is

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

Quoted 10 lines
kernel/sched/fair.c +2 -3
  		 * 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);
 
+
+		env.loop_max = min3(env.loop_max, sysctl_sched_nr_migrate, busiest->h_nr_running);

It should be busiest->nr_running? or businest->cfs.h_nr_queued?

do you mind I create a patch based on this one? or You create an official patch?

Thanks

Huang Shijie

maintainer
#4 lore raw
In reply to Valentin Schneider
Quoted 48 lines · Valentin Schneider
Quoted 8 lines · Vincent Guittot
Quoted 6 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, and besides detach_tasks() can be re-invoked after releasing and re-acquiring the RQ lock per LBF_NEED_BREAK.

Quoted 7 lines
Quoted 2 lines

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

why not setting env.loop_max only once rq lock is taken in this case ?

side note : by default loop_max <= loop_break

I thought so too and dismissed that due to LBF_NEED_BREAK, but I guess we could still do something like:

---

kernel/sched/fair.c +2 -2
index b9b4bbbf0af6f..eef3a0d341661 100644
@@ -11643,6 +11643,7 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
                .dst_grpmask    = group_balance_mask(sd->groups),
                .idle           = idle,
                .loop_break     = SCHED_NR_MIGRATE_BREAK,
+               .loop_max       = UINT_MAX,
                .cpus           = cpus,
                .fbq_type       = all,
                .tasks          = LIST_HEAD_INIT(env.tasks),
@@ -11681,18 +11682,19 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
        /* Clear this flag as soon as we find a pullable task */
        env.flags |= LBF_ALL_PINNED;
        if (busiest->nr_running > 1) {
+more_balance:
                /*
                 * Attempt to move tasks. If sched_balance_find_src_group has found
                 * an imbalance but busiest->nr_running <= 1, the group is
                 * 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);
-

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

Quoted 10 lines
kernel/sched/fair.c +3 -1
-more_balance:
                rq_lock_irqsave(busiest, &rf);
                update_rq_clock(busiest);
 
+
+               env.loop_max = min3(env.loop_max, sysctl_sched_nr_migrate, busiest->h_nr_running);
+
                /*
                 * cur_ld_moved - load moved in current iteration
                 * ld_moved     - cumulative load moved across iterations

I would prefer something like below:

kernel/sched/fair.c +0 -0
index 1b3879850a9e..636798d53798 100644
@@ -11702,12 +11702,16 @@ 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_runnable);
+ else
+ env.loop_max = min(env.loop_max,
busiest->cfs.h_nr_runnable);
+
/*
* cur_ld_moved - load moved in current iteration
* ld_moved - cumulative load moved across iterations

maintainer
#5 lore raw
Quoted 72 lines · Vincent Guittot
Quoted 48 lines · Valentin Schneider
Quoted 8 lines · Vincent Guittot
Quoted 6 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, and besides detach_tasks() can be re-invoked after releasing and re-acquiring the RQ lock per LBF_NEED_BREAK.

Quoted 7 lines
Quoted 2 lines

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

why not setting env.loop_max only once rq lock is taken in this case ?

side note : by default loop_max <= loop_break

I thought so too and dismissed that due to LBF_NEED_BREAK, but I guess we could still do something like:

---

kernel/sched/fair.c +2 -2
index b9b4bbbf0af6f..eef3a0d341661 100644
@@ -11643,6 +11643,7 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
                .dst_grpmask    = group_balance_mask(sd->groups),
                .idle           = idle,
                .loop_break     = SCHED_NR_MIGRATE_BREAK,
+               .loop_max       = UINT_MAX,
                .cpus           = cpus,
                .fbq_type       = all,
                .tasks          = LIST_HEAD_INIT(env.tasks),
@@ -11681,18 +11682,19 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
        /* Clear this flag as soon as we find a pullable task */
        env.flags |= LBF_ALL_PINNED;
        if (busiest->nr_running > 1) {
+more_balance:
                /*
                 * Attempt to move tasks. If sched_balance_find_src_group has found
                 * an imbalance but busiest->nr_running <= 1, the group is
                 * 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);
-

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

Quoted 10 lines
kernel/sched/fair.c +3 -1
-more_balance:
                rq_lock_irqsave(busiest, &rf);
                update_rq_clock(busiest);
 
+
+               env.loop_max = min3(env.loop_max, sysctl_sched_nr_migrate, busiest->h_nr_running);
+
                /*
                 * cur_ld_moved - load moved in current iteration
                 * ld_moved     - cumulative load moved across iterations

I would prefer something like below:

kernel/sched/fair.c +0 -0
index 1b3879850a9e..636798d53798 100644
@@ -11702,12 +11702,16 @@ 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.
                 */

kernel/sched/fair.c static int sched_balance_rq(int this_cpu, · line 11714

Quoted 10 lines
kernel/sched/fair.c +2 -1
-               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_runnable);

it should be h_nr_queued as mentioned by Huang and my patch has been messed up by my web browser

Quoted 7 lines
kernel/sched/fair.c +2 -0
+               else
+                       env.loop_max = min(env.loop_max,

busiest->cfs.h_nr_runnable);
+
/*
* cur_ld_moved - load moved in current iteration
* ld_moved - cumulative load moved across iterations

#6 lore raw
Quoted 16 lines · Vincent Guittot
Quoted 16 lines
kernel/sched/fair.c +0 -1
index 1b3879850a9e..636798d53798 100644
@@ -11702,12 +11702,16 @@ 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);
 
Quoted 7 lines

kernel/sched/fair.c static int sched_balance_rq(int this_cpu, · line 11714

Quoted 3 lines
kernel/sched/fair.c +2 -0
+               if (!env.loop_max)
+                       env.loop_max = min(sysctl_sched_nr_migrate,
 busiest->cfs.h_nr_runnable);

it should be h_nr_queued as mentioned by Huang and my patch has been messed up by my web browser

Works for me.

#7 lore raw
In reply to Vincent Guittot
Quoted 9 lines · Vincent Guittot
Quoted 7 lines
  more_balance:
                 rq_lock_irqsave(busiest, &rf);
                 update_rq_clock(busiest);
(quoted diff) +2 -0
+               if (!env.loop_max)
+                       env.loop_max = min(sysctl_sched_nr_migrate,

busiest->cfs.h_nr_runnable);

it should be h_nr_queued as mentioned by Huang and my patch has been messed up by my web browser

okay, I can create a patch later, and send it out after it passes the Specjbb test.

Thanks

Huang Shijie

#8 lore raw
In reply to Vincent Guittot
Quoted 10 lines · Vincent Guittot
Quoted 7 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, and besides detach_tasks() can be re-invoked after releasing and re-acquiring the RQ lock per LBF_NEED_BREAK.

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

why not setting env.loop_max only once rq lock is taken in this case ?

Yes.  we do it in this way.

Thanks

Huang Shijie