sched/fair: do not scan twice in detach_tasks()
PATCH 1/1 sched/fair: do not scan twice in detach_tasks()
When detach_tasks() scans the src_cpu's task list, the task list may shrink during the scanning. For example, the task list may have four tasks at the beginning, it may becomes to two during the scanning in detach_tasks():
Task list at beginning : "ABCD"
Task list in scanning : "CD"
(ABCD stands for differnt tasks.)
In this scenario, the env->loop_max is still four, so detach_tasks() may scan twice for some tasks:
the scanning order maybe : "DCDC"
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.
--- kernel/sched/fair.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/sched/fair.c +5 -0
@@ -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,8 @@ static int detach_tasks(struct lb_env *env) } p = list_last_entry(tasks, struct task_struct, se.group_node); +if (p == first_back) +break ;if (!can_migrate_task (p, env)) goto next;
@@ -9562,6 +9565,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
Valentin Schneider
reviewerQuoted 7 lines · Huang Shijie
When
detach_tasks()scans the src_cpu's task list, the task list may shrink during the scanning. For example, the task list may have four tasks at the beginning, it may becomes to two during the scanning indetach_tasks():
Task list at beginning : "ABCD"
Task list in scanning : "CD"
Quoted 6 lines
(ABCD stands for differnt tasks.)
In this scenario, the
env->loop_maxis still four, sodetach_tasks()may scan twice for some tasks:
the scanning order maybe : "DCDC"
Huh, a quick hacky test suggests this isn't /too/ hard to trigger; I get about one occurrence every two default hackbench run (~200ms) on my dummy QEMU setup.
Have you seen this happen on your workloads or did you find this while staring at the code?
Quoted 4 lines
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 tofirst_back, we break the loop, and avoid to scan twice for it.
Potentially more than twice, no?
Quoted 18 lines
Signed-off-by Huang Shijie <[email protected]>--- kernel/sched/fair.c | 5 +++++ 1 file changed, 5 insertions(+)kernel/sched/fair.c +1
-0@@ -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 ;
kernel/sched/fair.c static int detach_tasks(struct lb_env *env) · line 9484
Quoted 3 lines
kernel/sched/fair.c +0
-0@@ -9481,6 +9482,8 @@ static int detach_tasks(struct lb_env *env) } p = list_last_entry(tasks, struct task_struct, se.group_node);
Small comment nit:
/*
* We're back to an already visited task that couldn't be
* detached, we've seen all there is to see. */
Quoted 16 lines
kernel/sched/fair.c +4
-1+if (p == first_back) +break ;if (!can_migrate_task (p, env)) goto next;@@ -9562,6 +9565,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
Quoted 15 lines · Valentin Schneider
Quoted 7 lines · Huang Shijie
When
detach_tasks()scans the src_cpu's task list, the task list may shrink during the scanning. For example, the task list may have four tasks at the beginning, it may becomes to two during the scanning indetach_tasks():
Task list at beginning : "ABCD"
Task list in scanning : "CD"Quoted 6 lines
(ABCD stands for differnt tasks.)
In this scenario, the
env->loop_maxis still four, sodetach_tasks()may scan twice for some tasks:
the scanning order maybe : "DCDC"
Quoted 6 lines
Huh, a quick hacky test suggests this isn't /too/ hard to trigger; I get about one occurrence every two default hackbench run (~200ms) on my dummy QEMU setup.
Have you seen this happen on your workloads or did you find this while staring at the code?
I found this issue in my Specjbb2015 test. It is very easy to trigger.
I noticed many times in the test.
I even found that:
At the beginning: env->loop_max is 12.
When the detach_tasks() scans: the real task list shrink to 5.
Quoted 5 lines
Quoted 4 lines
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 tofirst_back, we break the loop, and avoid to scan twice for it.Potentially more than twice, no?
yes. it maybe more then twice in theory.
Quoted 21 lines
Quoted 21 lines
Signed-off-by Huang Shijie <[email protected]>--- kernel/sched/fair.c | 5 +++++ 1 file changed, 5 insertions(+)kernel/sched/fair.c +1
-0@@ -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,8 @@ static int detach_tasks(struct lb_env *env) }
Quoted 7 lines
kernel/sched/fair.c static int detach_tasks(struct lb_env *env) · line 9484
Quoted 1 line
kernel/sched/fair.c +0
-0p = list_last_entry(tasks, struct task_struct, se.group_node);Small comment nit:
/*
* We're back to an already visited task that couldn't be
* detached, we've seen all there is to see. */
Thanks, will use it in next version.
Thanks
Huang Shijie
Valentin Schneider
reviewerQuoted 16 lines · Shijie Huang
Quoted 15 lines · Valentin Schneider
Quoted 13 lines · Huang Shijie
When
detach_tasks()scans the src_cpu's task list, the task list may shrink during the scanning. For example, the task list may have four tasks at the beginning, it may becomes to two during the scanning indetach_tasks():
Task list at beginning : "ABCD"
Task list in scanning : "CD"(ABCD stands for differnt tasks.)
In this scenario, the
env->loop_maxis still four, sodetach_tasks()may scan twice for some tasks:
the scanning order maybe : "DCDC"
Quoted 9 lines
Quoted 6 lines
Huh, a quick hacky test suggests this isn't /too/ hard to trigger; I get about one occurrence every two default hackbench run (~200ms) on my dummy QEMU setup.
Have you seen this happen on your workloads or did you find this while staring at the code?
I found this issue in my Specjbb2015 test. It is very easy to trigger.
That would be good to include in the changelog.
Quoted 8 lines
I noticed many times in the test.
I even found that:
At the beginning:
env->loop_maxis 12.When the
detach_tasks()scans: the real task list shrink to 5.
That's set using rq->nr_running which includes more than just the CFS tasks, and looking at the git history it looks like it's almost always been the case.
Looks like env->loop_max really is only used for detach_tasks(), so perhaps a "better" fix would be something like the below, so that we can't iterate more than length(env->src_rq->cfs_tasks). That is, assuming
rq->cfs.h_nr_queued == length(rq->cfs_tasks)
---
kernel/sched/fair.c +1 -1
@@ -11687,7 +11687,7 @@ 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); + env.loop_max =min (sysctl_sched_nr_migrate, busiest->cfs.h_nr_queued); more_balance:rq_lock_irqsave (busiest, &rf);
Quoted 20 lines · Valentin Schneider
Quoted 19 lines · Shijie Huang
Quoted 18 lines · Valentin Schneider
Quoted 7 lines · Huang Shijie
When
detach_tasks()scans the src_cpu's task list, the task list may shrink during the scanning. For example, the task list may have four tasks at the beginning, it may becomes to two during the scanning indetach_tasks():
Task list at beginning : "ABCD"
Task list in scanning : "CD"Quoted 6 lines
(ABCD stands for differnt tasks.)
In this scenario, the
env->loop_maxis still four, sodetach_tasks()may scan twice for some tasks:
the scanning order maybe : "DCDC"Huh, a quick hacky test suggests this isn't /too/ hard to trigger; I get about one occurrence every two default hackbench run (~200ms) on my dummy QEMU setup.
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Have you seen this happen on your workloads or did you find this while staring at the code?
I found this issue in my Specjbb2015 test. It is very easy to trigger.
That would be good to include in the changelog.
okay.
Quoted 24 lines
Quoted 8 lines
I noticed many times in the test.
I even found that:
At the beginning:
env->loop_maxis 12.When the
detach_tasks()scans: the real task list shrink to 5.That's set using
rq->nr_runningwhich includes more than just the CFS tasks, and looking at the git history it looks like it's almost always been the case.Looks like
env->loop_maxreally is only used fordetach_tasks(), so perhaps a "better" fix would be something like the below, so that we can't iterate more thanlength(env->src_rq->cfs_tasks). That is, assumingrq->cfs.h_nr_queued == length(rq->cfs_tasks)---
kernel/sched/fair.c +0
-0
kernel/sched/fair.c static int sched_balance_rq(int this_cpu, struct rq *this_rq, · line 11690
Quoted 5 lines
kernel/sched/fair.c +1
-1@@ -11687,7 +11687,7 @@ 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); + env.loop_max =min (sysctl_sched_nr_migrate, busiest->cfs.h_nr_queued);
I tested this patch, it did not work. I still can catch lots of occurrences of this issue in Specjbb test.
IMHO, the root cause of this issue is env.loop_max is set out of the rq's lock.
Even we set env.loop_max to busiest->cfs.h_nr_queued, the real tasks length still can shrink in
other places.
Btw, the real tasks's length can also bigger then env.loop_max.
For example, we set env.loop_max with 5, when detach_tasks() runs, the real tasks' length can be changed to 7.
Thanks
Huang Shijie
Quoted 4 lines
kernel/sched/fair.c +0
-0more_balance:rq_lock_irqsave (busiest, &rf);
Valentin Schneider
reviewerQuoted 16 lines · Shijie Huang
kernel/sched/fair.c static int sched_balance_rq(int this_cpu, struct rq *this_rq, · line 11690
Quoted 10 lines · Valentin Schneider
kernel/sched/fair.c +1
-1@@ -11687,7 +11687,7 @@ 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); + env.loop_max =min (sysctl_sched_nr_migrate, busiest->cfs.h_nr_queued);I tested this patch, it did not work. I still can catch lots of occurrences of this issue in Specjbb test.
Quoted 8 lines
IMHO, the root cause of this issue is env.loop_max is set out of the rq's lock.
Even we set env.loop_max to
busiest->cfs.h_nr_queued, the real tasks length still can shrink inother places.
Ah right, and updating the max in detach_tasks() itself isn't a complete solution if we re-enter it due to LBF_NEED_BREAK. Nevermind then :-)