sched, fair: try to prevent migration thread from preempting non-cfs task
PATCH 1/1 sched, fair: try to prevent migration thread from preempting non-cfs task
We monitored our latency-sensitive RT tasks are randomly preempted by the kthread migration/n, which means to migrate task from CPUn to the new idle CPU which wakes up the migration/n. For example,
sensing_node-2511 [007] d... 945.351566: sched_switch: prev_comm=sensing_node prev_pid=2511 prev_prio=98 prev_state=S ==> next_comm=cat next_pid=2686 next_prio=120
cat-2686 [007] d... 945.351569: sched_switch: prev_comm=cat prev_pid=2686 prev_prio=120 prev_state=R+ ==> next_comm=sensing_node next_pid=2512 next_prio=98
sensing_node-2516 [004] dn.. 945.351571: sched_wakeup: comm=migration/7 pid=47 prio=0 target_cpu=007
sensing_node-2512 [007] d... 945.351572: sched_switch: prev_comm=sensing_node prev_pid=2512 prev_prio=98 prev_state=R ==> next_comm=migration/7 next_pid=47 next_prio=0
sensing_node-2516 [004] d... 945.351572: sched_switch: prev_comm=sensing_node prev_pid=2516 prev_prio=98 prev_state=S ==> next_comm=sensing_node next_pid=2502 next_prio=98
migration/7-47 [007] d... 945.351580: sched_switch: prev_comm=migration/7 prev_pid=47 prev_prio=0 prev_state=S ==> next_comm=sensing_node next_pid=2512 next_prio=98
sensing_node-2502 [004] d... 945.351605: sched_switch: prev_comm=sensing_node prev_pid=2502 prev_prio=98 prev_state=S ==> next_comm=cat next_pid=2686 next_prio=120When CPU4 is waking migration/7, the CFS thread 'cat' is running on CPU7, but then 'cat' is preempted by a RT task 'sensing_node', and then the migration/7 preempts the RT task. The race happens between:
if (need_active_balance(&env)) {
and
raw_spin_rq_lock_irqsave(busiest, flags);In order to reduce the race, we'd better do the last minute check before waking up migration thread.
---
- Prev version
https://lore.kernel.org/lkml/CAKfTPtBd349eyDhA5ThCAHFd83cGMQKb_LDxD4QvyP-cJOBjqA@mail.gmail.com/
- Similar discussion
https://lore.kernel.org/lkml/CAKfTPtBygNcVewbb0GQOP5xxO96am3YeTZNP5dK9BxKHJJAL-g@mail.gmail.com/
--- kernel/sched/fair.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
kernel/sched/fair.c +14 -0
@@ -9797,6 +9797,20 @@ static int load_balance(int this_cpu, struct rq *this_rq,/* Record that we found at least one task that could run on this_cpu */ env.flags &= ~LBF_ALL_PINNED ; +/* +* There may be a race between load balance starting migration +* thread to pull the cfs running thread and the RT thread +* waking up and preempting cfs task before migration threads +* which then preempt the RT thread. +* We'd better do the last minute check before starting +* migration thread to avoid preempting latency-sensitive thread. +*/ +if (busiest->curr->sched_class != &fair_sched_class) { +raw_spin_unlock_irqrestore (&busiest->lock, + flags); + goto out; + } +/* * ->active_balance synchronizes accesses to * ->active_balance_work. Once set, it's cleared
Signature · 1 line
2.17.1
Hi,
Quoted 5 lines · Yafang Shao
- Prev version
https://lore.kernel.org/lkml/CAKfTPtBd349eyDhA5ThCAHFd83cGMQKb_LDxD4QvyP-cJOBjqA@mail.gmail.com/- Similar discussion
https://lore.kernel.org/lkml/CAKfTPtBygNcVewbb0GQOP5xxO96am3YeTZNP5dK9BxKHJJAL-g@mail.gmail.com/
I knew that sounded familiar :-)
Quoted 10 lines
--- kernel/sched/fair.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)kernel/sched/fair.c +0
-0@@ -9797,6 +9797,20 @@ static int load_balance(int this_cpu, struct rq *this_rq,/* Record that we found at least one task that could run on this_cpu */
kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9807
Quoted 10 lines
kernel/sched/fair.c +8
-0env.flags &= ~LBF_ALL_PINNED ; +/* +* There may be a race between load balance starting migration +* thread to pull the cfs running thread and the RT thread +* waking up and preempting cfs task before migration threads +* which then preempt the RT thread. +* We'd better do the last minute check before starting +* migration thread to avoid preempting latency-sensitive thread. +*/
This can be summarized as in the below, no?
/*
* Don't cause a higher-than-CFS task to be preempted by
* the CPU stopper.
*/
kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9811
Quoted 4 lines
kernel/sched/fair.c +4
-0+if (busiest->curr->sched_class != &fair_sched_class) { +raw_spin_unlock_irqrestore (&busiest->lock, + flags); + goto out;
Since you goto out this could be moved before the
env.flags &= ~LBF_ALL_PINNED;
above (it only has an impact if you'd goto out_balanced).
kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9813
Quoted 2 lines
kernel/sched/fair.c +2
-0+ } +
Other than the above, this looks OK to me.
Back then I had argued that having a >CFS task and holding the remote rq lock could let us invoke detach_one_task() locally (rather than on the stopper thread), but realistically if we got to this !ld_moved condition then the chances of us actually pulling something here are very slim (we'd depend on enqueues happening between ~detach_tasks() and here).
Quoted 5 lines
kernel/sched/fair.c +0
-1/* * ->active_balance synchronizes accesses to * ->active_balance_work. Once set, it's cleared -- 2.17.1
Vincent Guittot
maintainerQuoted 37 lines · Valentin Schneider
Hi,
Quoted 5 lines · Yafang Shao
- Prev version
https://lore.kernel.org/lkml/CAKfTPtBd349eyDhA5ThCAHFd83cGMQKb_LDxD4QvyP-cJOBjqA@mail.gmail.com/- Similar discussion
https://lore.kernel.org/lkml/CAKfTPtBygNcVewbb0GQOP5xxO96am3YeTZNP5dK9BxKHJJAL-g@mail.gmail.com/I knew that sounded familiar :-)
Quoted 10 lines
--- kernel/sched/fair.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)kernel/sched/fair.c +0
-0@@ -9797,6 +9797,20 @@ static int load_balance(int this_cpu, struct rq *this_rq,/* Record that we found at least one task that could run on this_cpu */ kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9807
Quoted 10 lines
kernel/sched/fair.c +8
-0env.flags &= ~LBF_ALL_PINNED ; +/* +* There may be a race between load balance starting migration +* thread to pull the cfs running thread and the RT thread +* waking up and preempting cfs task before migration threads +* which then preempt the RT thread. +* We'd better do the last minute check before starting +* migration thread to avoid preempting latency-sensitive thread. +*/ This can be summarized as in the below, no?
Quoted 4 lines
/*
* Don't cause a higher-than-CFS task to be preempted by
* the CPU stopper.
*/
IMO, it's worth keeping the explanation that we are there because:
- a CFS task that was running during the 1st step : if (busiest->nr_running > 1) { ...
so we didn't pull the task
- but it has been preempted while lb was deciding if it needs an active load balance
so maybe something like:
/*
* Don't kick the active_load_balance_cpu_stop,
* if the CFS task has been preempted by higher
* priority task in the meantime.
*/
Quoted 11 lines
kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9811
Quoted 5 lines
kernel/sched/fair.c +4
-0+if (busiest->curr->sched_class != &fair_sched_class) { +raw_spin_unlock_irqrestore (&busiest->lock, + flags); + goto out;Since you goto out this could be moved before the
env.flags &= ~LBF_ALL_PINNED;above (it only has an impact if you'd goto
out_balanced).
Good point. My comment to move this test after env.flags &= ~LBF_ALL_PINNED; was valid only with goto out_one_pinned
Quoted 17 lines
kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9813
Quoted 3 lines
kernel/sched/fair.c +2
-0+ } +Other than the above, this looks OK to me.
Back then I had argued that having a >CFS task and holding the remote rq lock could let us invoke
detach_one_task()locally (rather than on the stopper thread), but realistically if we got to this !ld_moved condition then the chances of us actually pulling something here are very slim (we'd depend on enqueues happening between ~detach_tasks() and here).Quoted 5 lines
kernel/sched/fair.c +0
-1/* * ->active_balance synchronizes accesses to * ->active_balance_work. Once set, it's cleared -- 2.17.1
Quoted 16 lines · Vincent Guittot
Quoted 6 lines · Valentin Schneider
This can be summarized as in the below, no?
/*
* Don't cause a higher-than-CFS task to be preempted by
* the CPU stopper.
*/IMO, it's worth keeping the explanation that we are there because:
- a CFS task that was running during the 1st step : if (busiest->nr_running> 1) { ...
so we didn't pull the task
- but it has been preempted while lb was deciding if it needs an active load balance
Quoted 7 lines
so maybe something like:
/*
* Don't kick theactive_load_balance_cpu_stop,
* if the CFS task has been preempted by higher
* priority task in the meantime.
*/
Ack
Peter Zijlstra
maintainerQuoted 13 lines · Yafang Shao
--- kernel/sched/fair.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)kernel/sched/fair.c +1
-0@@ -9797,6 +9797,20 @@ static int load_balance(int this_cpu, struct rq *this_rq,/* Record that we found at least one task that could run on this_cpu */ env.flags &= ~LBF_ALL_PINNED ; +/*
kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9810
Quoted 10 lines
kernel/sched/fair.c +10
-0+* There may be a race between load balance starting migration +* thread to pull the cfs running thread and the RT thread +* waking up and preempting cfs task before migration threads +* which then preempt the RT thread. +* We'd better do the last minute check before starting +* migration thread to avoid preempting latency-sensitive thread. +*/ +if (busiest->curr->sched_class != &fair_sched_class) { +raw_spin_unlock_irqrestore (&busiest->lock, + flags);
This won't apply.
Also, there's still a race window, you've just shrunk it, not fixed it. Busiest can reschedule between the mandatory rq unlock and doing the stopper wakeup.
An actual fix might be to have the active migration done by a FIFO-1 task, instead of stopper. The obvious down-side is that that would mean spawning yet another per-cpu kthread.
How much do we care?
Quoted 28 lines · Peter Zijlstra
Quoted 13 lines · Yafang Shao
--- kernel/sched/fair.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)kernel/sched/fair.c +1
-0@@ -9797,6 +9797,20 @@ static int load_balance(int this_cpu, struct rq *this_rq,/* Record that we found at least one task that could run on this_cpu */ env.flags &= ~LBF_ALL_PINNED ; +/* kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9810
Quoted 10 lines
kernel/sched/fair.c +10
-0+* There may be a race between load balance starting migration +* thread to pull the cfs running thread and the RT thread +* waking up and preempting cfs task before migration threads +* which then preempt the RT thread. +* We'd better do the last minute check before starting +* migration thread to avoid preempting latency-sensitive thread. +*/ +if (busiest->curr->sched_class != &fair_sched_class) { +raw_spin_unlock_irqrestore (&busiest->lock, + flags);This won't apply.
Quoted 8 lines
Also, there's still a race window, you've just shrunk it, not fixed it. Busiest can reschedule between the mandatory rq unlock and doing the stopper wakeup.
An actual fix might be to have the active migration done by a FIFO-1 task, instead of stopper. The obvious down-side is that that would mean spawning yet another per-cpu kthread.
The stopper and the migration thread are different threads in the earlier days. commit 969c79215a35 ("sched: replace migration_thread with cpu_stop") merged them into one thread.
Regarding the priority of stopper (with highest priority) and migration (higher than CFS, but lower than RT) , keeping them in one single thread seems not a good way.
Quoted 1 line
How much do we care?
Signature · 2 lines
Thanks Yafang
Peter Zijlstra
maintainerQuoted 33 lines · Yafang Shao
Quoted 32 lines · Peter Zijlstra
Quoted 13 lines · Yafang Shao
--- kernel/sched/fair.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)kernel/sched/fair.c +1
-0@@ -9797,6 +9797,20 @@ static int load_balance(int this_cpu, struct rq *this_rq,/* Record that we found at least one task that could run on this_cpu */ env.flags &= ~LBF_ALL_PINNED ; +/* kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9810
Quoted 10 lines
kernel/sched/fair.c +10
-0+* There may be a race between load balance starting migration +* thread to pull the cfs running thread and the RT thread +* waking up and preempting cfs task before migration threads +* which then preempt the RT thread. +* We'd better do the last minute check before starting +* migration thread to avoid preempting latency-sensitive thread. +*/ +if (busiest->curr->sched_class != &fair_sched_class) { +raw_spin_unlock_irqrestore (&busiest->lock, + flags);This won't apply.
Also, there's still a race window, you've just shrunk it, not fixed it. Busiest can reschedule between the mandatory rq unlock and doing the stopper wakeup.
Quoted 8 lines
Quoted 4 lines
An actual fix might be to have the active migration done by a FIFO-1 task, instead of stopper. The obvious down-side is that that would mean spawning yet another per-cpu kthread.
The stopper and the migration thread are different threads in the earlier days. commit
969c79215a35("sched: replacemigration_threadwithcpu_stop") merged them into one thread.
Yes, I know, I was there. But that's not what I'm saying, we need the migration thread to be super high perio for other cases. That change still makes sense.
Quoted 3 lines
Regarding the priority of stopper (with highest priority) and migration (higher than CFS, but lower than RT) , keeping them in one single thread seems not a good way.
I never suggested as such.
Only the active migration of CFS can be done by a FIFO-1 task (the lowest prio that is higher than CFS) and possible the numa balancing thing.
Other migrations will still need to use stopper, and as such you'll keep having interference from stopper.
The suggestion was adding a cfs_migration thread, specifically for active balance (and maybe numa). Just not sure the cost of carrying yet another per-cpu kernel thread is worth the benefit.
Vincent Guittot
maintainerQuoted 58 lines · Peter Zijlstra
Quoted 33 lines · Yafang Shao
Quoted 32 lines · Peter Zijlstra
Quoted 13 lines · Yafang Shao
--- kernel/sched/fair.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)kernel/sched/fair.c +1
-0@@ -9797,6 +9797,20 @@ static int load_balance(int this_cpu, struct rq *this_rq,/* Record that we found at least one task that could run on this_cpu */ env.flags &= ~LBF_ALL_PINNED ; +/* kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9810
Quoted 10 lines
kernel/sched/fair.c +10
-0+* There may be a race between load balance starting migration +* thread to pull the cfs running thread and the RT thread +* waking up and preempting cfs task before migration threads +* which then preempt the RT thread. +* We'd better do the last minute check before starting +* migration thread to avoid preempting latency-sensitive thread. +*/ +if (busiest->curr->sched_class != &fair_sched_class) { +raw_spin_unlock_irqrestore (&busiest->lock, + flags);This won't apply.
Also, there's still a race window, you've just shrunk it, not fixed it. Busiest can reschedule between the mandatory rq unlock and doing the stopper wakeup.
Quoted 8 lines
Quoted 4 lines
An actual fix might be to have the active migration done by a FIFO-1 task, instead of stopper. The obvious down-side is that that would mean spawning yet another per-cpu kthread.
The stopper and the migration thread are different threads in the earlier days. commit
969c79215a35("sched: replacemigration_threadwithcpu_stop") merged them into one thread.Yes, I know, I was there. But that's not what I'm saying, we need the migration thread to be super high perio for other cases. That change still makes sense.
Quoted 3 lines
Regarding the priority of stopper (with highest priority) and migration (higher than CFS, but lower than RT) , keeping them in one single thread seems not a good way.
I never suggested as such.
Only the active migration of CFS can be done by a FIFO-1 task (the lowest prio that is higher than CFS) and possible the numa balancing thing.
Quoted 6 lines
Other migrations will still need to use stopper, and as such you'll keep having interference from stopper.
The suggestion was adding a
cfs_migrationthread, specifically for active balance (and maybe numa). Just not sure the cost of carrying yet another per-cpu kernel thread is worth the benefit.
Also, this will not completely remove the problem but only further reduce the race window because the rq is locked and the irq disable in active_load_balance_cpu_stop().
Peter Zijlstra
maintainerQuoted 9 lines · Vincent Guittot
Quoted 3 lines · Peter Zijlstra
The suggestion was adding a
cfs_migrationthread, specifically for active balance (and maybe numa). Just not sure the cost of carrying yet another per-cpu kernel thread is worth the benefit.Also, this will not completely remove the problem but only further reduce the race window because the rq is locked and the irq disable in
active_load_balance_cpu_stop().
It removes the problem of active migration interfering with this worklaod, because the FIFO1 task will never run until that is done (assuming he manages to not have his workload at FIFO1).
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Vincent Guittot
Quoted 3 lines · Peter Zijlstra
The suggestion was adding a
cfs_migrationthread, specifically for active balance (and maybe numa). Just not sure the cost of carrying yet another per-cpu kernel thread is worth the benefit.
Quoted 8 lines
Quoted 3 lines
Also, this will not completely remove the problem but only further reduce the race window because the rq is locked and the irq disable in
active_load_balance_cpu_stop().It removes the problem of active migration interfering with this worklaod, because the FIFO1 task will never run until that is done (assuming he manages to not have his workload at FIFO1).
Right, the workload should have a higher priority than FIFO1 then.
I'm wondering why not just setting some flags to the running CFS and then when the CFS task scheds out the CPU we migrate it to the new idle CPU in active LB. Then we don't need to preempt any task.
Signature · 2 lines
Thanks Yafang
Quoted 58 lines · Peter Zijlstra
Quoted 33 lines · Yafang Shao
Quoted 32 lines · Peter Zijlstra
Quoted 13 lines · Yafang Shao
--- kernel/sched/fair.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)kernel/sched/fair.c +1
-0@@ -9797,6 +9797,20 @@ static int load_balance(int this_cpu, struct rq *this_rq,/* Record that we found at least one task that could run on this_cpu */ env.flags &= ~LBF_ALL_PINNED ; +/* kernel/sched/fair.c static int load_balance(int this_cpu, struct rq *this_rq, · line 9810
Quoted 10 lines
kernel/sched/fair.c +10
-0+* There may be a race between load balance starting migration +* thread to pull the cfs running thread and the RT thread +* waking up and preempting cfs task before migration threads +* which then preempt the RT thread. +* We'd better do the last minute check before starting +* migration thread to avoid preempting latency-sensitive thread. +*/ +if (busiest->curr->sched_class != &fair_sched_class) { +raw_spin_unlock_irqrestore (&busiest->lock, + flags);This won't apply.
Also, there's still a race window, you've just shrunk it, not fixed it. Busiest can reschedule between the mandatory rq unlock and doing the stopper wakeup.
Quoted 8 lines
Quoted 4 lines
An actual fix might be to have the active migration done by a FIFO-1 task, instead of stopper. The obvious down-side is that that would mean spawning yet another per-cpu kthread.
The stopper and the migration thread are different threads in the earlier days. commit
969c79215a35("sched: replacemigration_threadwithcpu_stop") merged them into one thread.Yes, I know, I was there. But that's not what I'm saying, we need the migration thread to be super high perio for other cases. That change still makes sense.
Quoted 3 lines
Regarding the priority of stopper (with highest priority) and migration (higher than CFS, but lower than RT) , keeping them in one single thread seems not a good way.
I never suggested as such.
Only the active migration of CFS can be done by a FIFO-1 task (the lowest prio that is higher than CFS) and possible the numa balancing thing.
Quoted 6 lines
Other migrations will still need to use stopper, and as such you'll keep having interference from stopper.
The suggestion was adding a
cfs_migrationthread, specifically for active balance (and maybe numa). Just not sure the cost of carrying yet another per-cpu kernel thread is worth the benefit.
Thanks for the clarification.
Signature · 2 lines
Thanks Yafang