(Was: sched: start stopper early)
cover (Was: sched: start stopper early)
To avoid the confusion, this has nothing to do with "stop_machine" changes we discuss in another thread, but
Quoted 16 lines · Oleg Nesterov
Review static int sched_cpu_active(struct notifier_block *nfb, · line 5343
Quoted 12 lines
(quoted diff) +2
-1case CPU_ONLINE: + stop_machine_unpark(cpu); /* * At this point a starting CPU has marked itself as online via * set_cpu_online(). But it might not yet have marked itself@@ -5337,7 +5340,7 @@ static int sched_cpu_active(struct notifier_block *nfb, * Thus, fall-through and help the starting CPU along. */ case CPU_DOWN_FAILED: - set_cpu_active((long)hcpu, true); + set_cpu_active(cpu, true);On a second thought, we can't do this (and your initial change has the same problem).
Quoted 3 lines
We can not wakeup it before
set_cpu_active(). This can lead to the same problem fixed bydd9d3843755da95f6"sched: Fix cpu_active_mask/cpu_online_maskrace".
OTOH, I don't understand why do we actually need this fix... Or, iow I don't really understand the cpu_active() checks in select_fallback_rq().
Looks like we have some strange arch/ which has the "unsafe" online && !active window, but then it is not clear why it is safe to mark it active in sched_cpu_active(CPU_ONLINE). Confused.
And I am even more confused by the fact that select_fallback_rq() checks cpu_active(), but select_task_rq() doesn't. This can't be right in any case.
Oleg.
kernel/sched/core.c | 41 +++++++++++++++++++++-------------------- 1 files changed, 21 insertions(+), 20 deletions(-)
PATCH 1/3 sched: select_task_rq() should check cpu_active() like select_fallback_rq()
I do not understand the cpu_active() check in select_fallback_rq(). x86 doesn't need it, and the recent commit dd9d3843755d "sched: Fix cpu_active_mask/cpu_online_mask race" documents the fact that on any architecture we can ignore !active starting from CPU_ONLINE stage.
But any possible reason why do we need this check in "fallback" must equally apply to select_task_rq().
--- kernel/sched/core.c | 15 ++++++++------- 1 files changed, 8 insertions(+), 7 deletions(-)
kernel/sched/core.c +8 -7
@@ -1297,6 +1297,11 @@ void kick_process(struct task_struct *p) }EXPORT_SYMBOL_GPL (kick_process); +static inline bool cpu_allowed (int cpu) +{ +return cpu_online (cpu) &&cpu_active (cpu); +} +/* * ->cpus_allowed is protected by both rq->lock and p->pi_lock */
@@ -1317,9 +1322,7 @@ static int select_fallback_rq(int cpu, struct task_struct *p)/* Look for allowed, online CPU in same node. */ for_each_cpu (dest_cpu, nodemask) { -if (!cpu_online (dest_cpu)) -continue ; -if (!cpu_active (dest_cpu)) +if (!cpu_allowed (dest_cpu))continue ;if (cpumask_test_cpu (dest_cpu,tsk_cpus_allowed (p)))return dest_cpu;
@@ -1329,9 +1332,7 @@ static int select_fallback_rq(int cpu, struct task_struct *p)for (;;) {/* Any allowed, online CPU? */ for_each_cpu (dest_cpu,tsk_cpus_allowed (p)) { -if (!cpu_online (dest_cpu)) -continue ; -if (!cpu_active (dest_cpu)) +if (!cpu_allowed (dest_cpu))continue ; goto out; }
@@ -1390,7 +1391,7 @@ int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)* not worry about this generic constraint ] */ if (unlikely (!cpumask_test_cpu (cpu,tsk_cpus_allowed (p)) || - !cpu_online (cpu))) + !cpu_allowed (cpu))) cpu =select_fallback_rq (task_cpu (p), p);return cpu;
Signature · 1 line
1.5.5.1
Re: [PATCH 1/3] sched: select_task_rq() should check cpu_active() like select_fallback_rq()
Quoted 8 lines · Oleg Nesterov
I do not understand the
cpu_active()check inselect_fallback_rq(). x86 doesn't need it, and the recent commitdd9d3843755d"sched: Fix cpu_active_mask/cpu_online_mask race" documents the fact that on any architecture we can ignore !active starting fromCPU_ONLINEstage.But any possible reason why do we need this check in "fallback" must equally apply to
select_task_rq().
And I still think this is true, select_task_rq() and select_fallback_rq() should use the same check in any case...
Quoted 4 lines
kernel/sched/core.c +4
-0+static inline bool cpu_allowed (int cpu) +{ +return cpu_online (cpu) &&cpu_active (cpu); +}
...
kernel/sched/core.c int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags) · line 1395
Quoted 7 lines
kernel/sched/core.c +1
-1@@ -1390,7 +1391,7 @@ int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)* not worry about this generic constraint ] */ if (unlikely (!cpumask_test_cpu (cpu,tsk_cpus_allowed (p)) || - !cpu_online (cpu))) + !cpu_allowed (cpu))) cpu =select_fallback_rq (task_cpu (p), p);
But as Fengguang reports (thanks a lot!) this change is wrong. It leads to another BUG_ON(td->cpu != smp_processor_id()) before ht->park(td->cpu) in smpboot_thread_fn().
I should have realized this. smpboot_park_threads() is called after CPU_DOWN_PREPARE. And this can break other PF_NO_SETAFFINITY threads.
Perhaps I am totally confused, but to me this looks like another indication that select_fallback_rq() should not check cpu_active(), or at least this needs some changes...
Oleg.
Re: [PATCH 1/3] sched: select_task_rq() should check cpu_active() like select_fallback_rq()
Quoted 30 lines · Oleg Nesterov
Quoted 8 lines · Oleg Nesterov
I do not understand the
cpu_active()check inselect_fallback_rq(). x86 doesn't need it, and the recent commitdd9d3843755d"sched: Fix cpu_active_mask/cpu_online_mask race" documents the fact that on any architecture we can ignore !active starting fromCPU_ONLINEstage.But any possible reason why do we need this check in "fallback" must equally apply to
select_task_rq().And I still think this is true,
select_task_rq()andselect_fallback_rq()should use the same check in any case...Quoted 4 lines
kernel/sched/core.c +4
-0+static inline bool cpu_allowed (int cpu) +{ +return cpu_online (cpu) &&cpu_active (cpu); +}...
kernel/sched/core.c int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags) · line 1395
Quoted 7 lines
kernel/sched/core.c +1
-1@@ -1390,7 +1391,7 @@ int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)* not worry about this generic constraint ] */ if (unlikely (!cpumask_test_cpu (cpu,tsk_cpus_allowed (p)) || - !cpu_online (cpu))) + !cpu_allowed (cpu))) cpu =select_fallback_rq (task_cpu (p), p);But as Fengguang reports (thanks a lot!) this change is wrong. It leads to another
BUG_ON(td->cpu != smp_processor_id())beforeht->park(td->cpu)insmpboot_thread_fn().
Quoted 6 lines
I should have realized this.
smpboot_park_threads()is called afterCPU_DOWN_PREPARE. And this can break otherPF_NO_SETAFFINITYthreads.Perhaps I am totally confused, but to me this looks like another indication that
select_fallback_rq()should not checkcpu_active(), or at least this needs some changes...
Well, the real issue is that the bringup and teardown of cpus is completely assymetric. And as long as we do not fix that, we'll run into that kind of trouble over and over. We just add more duct tape to the cpu hotplug code.
The solution to the problem at hand is to have two separate decisions for threads to be scheduled on a upcoming or downgoing cpu.
We need to allow per cpu kernel threads to be scheduled after the initial bringup is done and on teardown we must allow them to be scheduled to the point where the cpu is actually not longer capable to do so.
For everything else the decision must be at the end of the bringup. From that point on random threads can be scheduled on the cpu. On teardown we need to prevent that as the first thing.
We are currently resurrecting the hotplug revamp patch series, which I sent out a couple of years ago. The goal of this is to make it completely symmetric and some more to address exactly this kind of trouble.
Thanks,
tglx
Peter Zijlstra
maintainerRe: [PATCH 1/3] sched: select_task_rq() should check cpu_active() like select_fallback_rq()
In reply to Oleg NesterovQuoted 7 lines · Oleg Nesterov
I do not understand the
cpu_active()check inselect_fallback_rq(). x86 doesn't need it, and the recent commitdd9d3843755d"sched: Fix cpu_active_mask/cpu_online_mask race" documents the fact that on any architecture we can ignore !active starting fromCPU_ONLINEstage.But any possible reason why do we need this check in "fallback" must equally apply to
select_task_rq().
So the reason, from vague memory, is that we want to allow per-cpu threads to start/stop before/after active.
active 'should' really only govern load-balancer bits or so.
Re: [PATCH 1/3] sched: select_task_rq() should check cpu_active() like select_fallback_rq()
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Oleg Nesterov
I do not understand the
cpu_active()check inselect_fallback_rq(). x86 doesn't need it, and the recent commitdd9d3843755d"sched: Fix cpu_active_mask/cpu_online_mask race" documents the fact that on any architecture we can ignore !active starting fromCPU_ONLINEstage.
Quoted 5 lines
Quoted 2 lines
But any possible reason why do we need this check in "fallback" must equally apply to
select_task_rq().So the reason, from vague memory, is that we want to allow per-cpu threads to start/stop before/after active.
I simply can't understand... To me it looks as if we can simply remove the cpu_active() check in select_fallback_rq().
If we race with cpu_down(), cpu_active() is cleared by sched_cpu_inactive() which is CPU_PRI_SCHED_INACTIVE = INT_MIN + 1 priority, so it seems that only cpuset_cpu_inactive() can be called after that and this check is obviously racy anyway.
As for cpu_up(), I do not see any arch which does set_cpu_active(true), they all do set_cpu_online(true) which also marks it active.
So why we can't simply remove select_fallback_rq()->cpu_active() ?
Quoted 1 line
active 'should' really only govern load-balancer bits or so.
OK, I don't understand the usage of cpu_active_mask in kernel/sched/, and of course I could easily miss something else. But I doubt very much this check can save us from something bad simply because it is racy.
Yes, we also have synchronize_sched() after CPU_DOWN_PREPARE, but the only thing we do before stop_machine() is smpboot_park_threads(). So this can help (say) set_cpus_allowed_ptr() which uses active_mask, but I don't see how this can connect to ttwu paths.
And again. If there is some reason we can not do this, say, because ipi to non-active CPU can trigger a warning, or something else, then we can hit the same problem because select_task_rq() does not check cpu_active(). The kernel threads like stoppers/workers are probably fine in any case, but userspace can trigger the race with cpu_up/down.
In short, I am all confused ;)
Oleg.
Peter Zijlstra
maintainerRe: [PATCH 1/3] sched: select_task_rq() should check cpu_active() like select_fallback_rq()
Quoted 14 lines · Oleg Nesterov
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Oleg Nesterov
I do not understand the
cpu_active()check inselect_fallback_rq(). x86 doesn't need it, and the recent commitdd9d3843755d"sched: Fix cpu_active_mask/cpu_online_mask race" documents the fact that on any architecture we can ignore !active starting fromCPU_ONLINEstage.Quoted 5 lines
Quoted 2 lines
But any possible reason why do we need this check in "fallback" must equally apply to
select_task_rq().So the reason, from vague memory, is that we want to allow per-cpu threads to start/stop before/after active.
Quoted 7 lines
I simply can't understand... To me it looks as if we can simply remove the
cpu_active()check inselect_fallback_rq().If we race with
cpu_down(),cpu_active()is cleared bysched_cpu_inactive()which isCPU_PRI_SCHED_INACTIVE=INT_MIN+ 1 priority, so it seems that onlycpuset_cpu_inactive()can be called after that and this check is obviously racy anyway.
Racy yes; however select_task_rq() is called with locks held, therefore preemption disabled. This serializes us against the sync_sched() in cpu_down().
And note that smpboot_park_threads() runs until after the sync_sched(). So you cannot add cpu_active() to select_task_rq() for it must allow per-cpu tasks to remain on the cpu.
Also, I think smpboot_park_threads() is called too early, we can still run random tasks at that point which might rely on having the per-cpu tasks around. But we cannot run it later because once the stopper thread runs, the per-cpu threads cannot schedule to park themselves :/
Lets keep an eye on Thomas' rework to see if this gets sorted.
Quoted 1 line
So why we can't simply remove select_fallback_rq()->cpu_active() ?
It would allow migration onto a CPU that's known to be going away. That doesn't make sense.
Quoted 6 lines
Quoted 1 line
active 'should' really only govern load-balancer bits or so.
OK, I don't understand the usage of
cpu_active_maskinkernel/sched/, and of course I could easily miss something else. But I doubt very much this check can save us from something bad simply because it is racy.
But safely so; if we observe active, it must stay 'active' until we enable preemption again.
The whole point of active is to limit the load-balancer; such that we can rebuild the sched-domains after the fact. We used to have to rebuild to the sched-domains early, which got into trouble (forgot details, again).
And we had to have a new mask, because online was too late, there was (forgot details) a state where we were still online but new are not welcome.
Also, it makes sense to stop accepting new tasks before you take out the old ones.
Quoted 4 lines
Yes, we also have
synchronize_sched()afterCPU_DOWN_PREPARE, but the only thing we do beforestop_machine()issmpboot_park_threads(). So this can help (say)set_cpus_allowed_ptr()which usesactive_mask, but I don't see how this can connect to ttwu paths.
Say you have a task A running on CPU4, it goes to sleep. We unplug CPU4. We then commence unplugging CPU3, concurrently we wake A. A finds that its old CPU (4) is no longer online and ends up in fallback looking for a new one.
Without the cpu_active() test in fallback, we could place A on CPU3, which is on its way down, just not quite dead.
Even if this is not strictly buggy its a daft thing to do and tempting fate.
Quoted 5 lines
And again. If there is some reason we can not do this, say, because ipi to non-active CPU can trigger a warning, or something else, then we can hit the same problem because
select_task_rq()does not checkcpu_active(). The kernel threads like stoppers/workers are probably fine in any case, but userspace can trigger the race with cpu_up/down.
So the only 'race' is observing active while we're stuck in sync_sched(), which is meant to be safe. It also provides us with a known spot after which we're sure no new tasks will come onto our dying CPU.
Re: [PATCH 1/3] sched: select_task_rq() should check cpu_active() like select_fallback_rq()
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · Oleg Nesterov
I simply can't understand... To me it looks as if we can simply remove the
cpu_active()check inselect_fallback_rq().
Quoted 8 lines
Quoted 4 lines
If we race with
cpu_down(),cpu_active()is cleared bysched_cpu_inactive()which isCPU_PRI_SCHED_INACTIVE=INT_MIN+ 1 priority, so it seems that onlycpuset_cpu_inactive()can be called after that and this check is obviously racy anyway.Racy yes; however
select_task_rq()is called with locks held, therefore preemption disabled. This serializes us against thesync_sched()incpu_down().
Yes, I even mentioned this below. And this only means that if you see cpu_active() == T you know that smpboot_park_threads() can't be called until preempt_disable().
cpu_active() can become false right after the check, preempt_disable() can't protect from __cpu_notify(CPU_DOWN_PREPARE).
Quoted 3 lines
And note that
smpboot_park_threads()runs until after thesync_sched(). So you cannot addcpu_active()toselect_task_rq()for it must allow per-cpu tasks to remain on the cpu.
Yes, yes, this is why this patch triggers BUG_ON() before ->park() in smpboot_thread_fn().
Quoted 4 lines
Quoted 1 line
So why we can't simply remove select_fallback_rq()->cpu_active() ?
It would allow migration onto a CPU that's known to be going away. That doesn't make sense.
But this is not that bad? This is very unlikely and CPU_DYING will migrate the woken task again.
Quoted 7 lines
Quoted 4 lines
OK, I don't understand the usage of
cpu_active_maskinkernel/sched/, and of course I could easily miss something else. But I doubt very much this check can save us from something bad simply because it is racy.But safely so; if we observe active, it must stay 'active' until we enable preemption again.
I don't undertand what "stay active" actually means here. cpu_active() is not stable. But probably this doesn't matter.
Quoted 5 lines
Quoted 4 lines
Yes, we also have
synchronize_sched()afterCPU_DOWN_PREPARE, but the only thing we do beforestop_machine()issmpboot_park_threads(). So this can help (say)set_cpus_allowed_ptr()which usesactive_mask, but I don't see how this can connect to ttwu paths.
Quoted 7 lines
Say you have a task A running on CPU4, it goes to sleep. We unplug CPU4. We then commence unplugging CPU3, concurrently we wake A. A finds that its old CPU (4) is no longer online and ends up in fallback looking for a new one.
Without the
cpu_active()test in fallback, we could place A on CPU3, which is on its way down, just not quite dead.
The same can happen with the cpu_active() check we currently have. And note again that CPU_PRI_SCHED_INACTIVE == INT_MIN + 1. When sched_cpu_inactive() clears cpu_active() all other callbacks (except cpuset_cpu_inactive() were already fired.
Quoted 2 lines
Even if this is not strictly buggy its a daft thing to do and tempting fate.
See above...
And if we remove this check from select_fallback_rq() we can revert dd9d3843755da95f6 "sched: Fix cpu_active_mask/cpu_online_mask race".
And revert 875ebe94 "powerpc/smp: Wait until secondaries are active & online". And a1307bba "s390/smp: wait until secondaries are active & online".
Quoted 8 lines
Quoted 5 lines
And again. If there is some reason we can not do this, say, because ipi to non-active CPU can trigger a warning, or something else, then we can hit the same problem because
select_task_rq()does not checkcpu_active(). The kernel threads like stoppers/workers are probably fine in any case, but userspace can trigger the race with cpu_up/down.So the only 'race' is observing active while we're stuck in
sync_sched(),
Why? select_task_rq() will see cpu_online() == T after sync_sched().
Quoted 1 line
which is meant to be safe.
Yes, because that damn cpu_active() check doesn't look strictly necessary ;) Or I misunderstood.
Quoted 3 lines
It also provides us with a
known spot after which we're sure no new tasks will come onto our dying CPU.
You mean that ttwu(task) can't migrate this task to the dying CPU after synchronize_rcu() and before stop_machine(), this is true.
OK, lets keep this check if you dislike the idea to remove it. But to me the very fact that select_task_rq() and select_fallback_rq() use different checks looks just wrong. Even if everything happens to work.
Oleg.
Peter Zijlstra
maintainerRe: [PATCH 1/3] sched: select_task_rq() should check cpu_active() like select_fallback_rq()
Quoted 2 lines · Oleg Nesterov
Yes, because that damn
cpu_active()check doesn't look strictly necessary ;) Or I misunderstood.
How about we sit down and have a hard look after Thomas is done revamping hotplug? I don't want to go pour over hotplug code that is guaranteed to change.
PATCH 2/3 sched: change select_fallback_rq() to use for_each_cpu_and()
In reply to Oleg NesterovWe can make "cpumask *nodemask" more local and use for_each_cpu_and() to simplify this code a little bit.
And NUMA_NO_NODE looks better than "-1".
--- kernel/sched/core.c | 14 ++++++-------- 1 files changed, 6 insertions(+), 8 deletions(-)
kernel/sched/core.c +6 -8
@@ -1308,24 +1308,22 @@ static inline bool cpu_allowed(int cpu)static int select_fallback_rq (int cpu,struct task_struct *p) {int nid =cpu_to_node (cpu); -const struct cpumask *nodemask =NULL ;enum { cpuset, possible, fail } state = cpuset;int dest_cpu;/* * If the node that the cpu is on has been offlined, cpu_to_node() -* will return -1. There is no cpu on the node, and we should -* select the cpu on the other node. +* will return NUMA_NO_NODE. There is no cpu on the node, and we +* should select the cpu on the other node. */ -if (nid !=-1 ) { - nodemask =cpumask_of_node (nid); +if (nid !=NUMA_NO_NODE ) { +const struct cpumask *nodemask =cpumask_of_node (nid);/* Look for allowed, online CPU in same node. */ -for_each_cpu (dest_cpu, nodemask) { +for_each_cpu_and (dest_cpu, nodemask,tsk_cpus_allowed (p)) {if (!cpu_allowed (dest_cpu))continue ; -if (cpumask_test_cpu (dest_cpu,tsk_cpus_allowed (p))) -return dest_cpu; +return dest_cpu; } }
Signature · 1 line
1.5.5.1
PATCH 3/3 sched: don't scan all-offline ->cpus_allowed twice if !CONFIG_CPUSETS
In reply to Oleg NesterovIf CONFIG_CPUSETS=n then "case cpuset" changes the state and runs the already failed for_each_cpu() loop again for no reason.
--- kernel/sched/core.c | 12 +++++++----- 1 files changed, 7 insertions(+), 5 deletions(-)
kernel/sched/core.c +7 -5
@@ -1335,13 +1335,15 @@ static int select_fallback_rq(int cpu, struct task_struct *p) goto out; } +/* No more Mr. Nice Guy. */ switch (state) {case cpuset: -/* No more Mr. Nice Guy. */ -cpuset_cpus_allowed_fallback (p); - state = possible; -break ; - +if (IS_ENABLED (CONFIG_CPUSETS )) { +cpuset_cpus_allowed_fallback (p); + state = possible; +break ; + } +/* fall-through */ case possible:do_set_cpus_allowed (p, cpu_possible_mask); state = fail;
Signature · 1 line
1.5.5.1
tip-bot for Oleg Nesterov
merge bot[tip:sched/core] sched: Don't scan all-offline -> cpus_allowed twice if !CONFIG_CPUSETS
sched: Don't scan all-offline ->cpus_allowed twice if !CONFIG_CPUSETS
Commit-ID: e73e85f0593832aa583b252f9a16cf90ed6d30fa Gitweb: http://git.kernel.org/tip/e73e85f0593832aa583b252f9a16cf90ed6d30fa Author: Oleg Nesterov <[email protected]> AuthorDate: Sat, 10 Oct 2015 20:53:15 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 20 Oct 2015 10:25:57 +0200
sched: Don't scan all-offline ->cpus_allowed twice if !CONFIG_CPUSETS
If CONFIG_CPUSETS=n then "case cpuset" changes the state and runs the already failed for_each_cpu() loop again for no reason.
--- kernel/sched/core.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
kernel/sched/core.c +7 -5
@@ -1580,13 +1580,15 @@ static int select_fallback_rq(int cpu, struct task_struct *p) goto out; } +/* No more Mr. Nice Guy. */ switch (state) {case cpuset: -/* No more Mr. Nice Guy. */ -cpuset_cpus_allowed_fallback (p); - state = possible; -break ; - +if (IS_ENABLED (CONFIG_CPUSETS )) { +cpuset_cpus_allowed_fallback (p); + state = possible; +break ; + } +/* fall-through */ case possible:do_set_cpus_allowed (p, cpu_possible_mask); state = fail;