sched: Use resched IPI to kick off the nohz idle balance
PATCH v2 1/2 sched: Use resched IPI to kick off the nohz idle balance
Current use of smp call function to kick the nohz idle balance can deadlock in this scenario.
1. cpu-A did a generic_exec_single() to cpu-B and after queuing its call single data (csd) to the call single queue, cpu-A took a timer interrupt. Actual IPI to cpu-B to process the call single queue is not yet sent.
2. As part of the timer interrupt handler, cpu-A decided to kick cpu-B for the idle load balancing (sets cpu-B's rq->nohz_balance_kick to 1) and __smp_call_function_single() with nowait will queue the csd to the cpu-B's queue. But the generic_exec_single() won't send an IPI to cpu-B as the call single queue was not empty.
3. cpu-A is busy with lot of interrupts
4. Meanwhile cpu-B is entering and exiting idle and noticed that it has it's rq->nohz_balance_kick set to '1'. So it will go ahead and do the idle load balancer and clear its rq->nohz_balance_kick.
5. At this point, csd queued as part of the step-2 above is still locked and waiting to be serviced on cpu-B.
6. cpu-A is still busy with interrupt load and now it got another timer interrupt and as part of it decided to kick cpu-B for another idle load balancing (as it finds cpu-B's rq->nohz_balance_kick cleared in step-4 above) and does __smp_call_function_single() with the same csd that is still locked.
7. And we get a deadlock waiting for the csd_lock() in the __smp_call_function_single().
Main issue here is that cpu-B can service the idle load balancer kick request from cpu-A even with out receiving the IPI and this lead to doing multiple __smp_call_function_single() on the same csd leading to deadlock.
To kick a cpu, scheduler already has the reschedule vector reserved. Use that mechanism (kick_process()) instead of using the generic smp call function mechanism to kick off the nohz idle load balancing and avoid the deadlock.
[ This issue is present from 2.6.35+ kernels, but marking it -stable
only from v3.0+ as the proposed fix depends on the scheduler_ipi() that is introduced recently. ]
--- kernel/sched.c | 21 +++++++++++++++++++-- kernel/sched_fair.c | 29 +++++++++-------------------- 2 files changed, 28 insertions(+), 22 deletions(-)
kernel/sched.c +19 -2
@@ -1404,6 +1404,18 @@ void wake_up_idle_cpu(int cpu) smp_send_reschedule(cpu); } +static inline bool got_nohz_idle_kick (void ) +{ +return idle_cpu (smp_processor_id ()) &&this_rq ()->nohz_balance_kick; +} + +#else /* CONFIG_NO_HZ */ + +static inline bool got_nohz_idle_kick (void ) +{ +return false; +} +#endif /* CONFIG_NO_HZ */ static u64 sched_avg_period (void )
@@ -2717,7 +2729,7 @@ static void sched_ttwu_pending(void)void scheduler_ipi (void ) { -if (llist_empty (&this_rq ()->wake_list)) +if (llist_empty (&this_rq ()->wake_list) && !got_nohz_idle_kick ())return ;/*
@@ -2735,6 +2747,12 @@ void scheduler_ipi(void)*/ irq_enter ();sched_ttwu_pending (); + +/* +* Check if someone kicked us for doing the nohz idle load balance. +*/ +if (unlikely (got_nohz_idle_kick () && !need_resched ())) +raise_softirq_irqoff (SCHED_SOFTIRQ );irq_exit (); }
@@ -8280,7 +8298,6 @@ void __init sched_init(void)rq_attach_root (rq, &def_root_domain);#ifdef CONFIG_NO_HZ rq->nohz_balance_kick =0 ; -init_sched_softirq_csd (&per_cpu (remote_sched_softirq_cb, i));#endif #endif init_rq_hrtick (rq);
kernel/sched_fair.c +9 -20
@@ -4269,22 +4269,6 @@ out_unlock: }#ifdef CONFIG_NO_HZ - -static DEFINE_PER_CPU(struct call_single_data, remote_sched_softirq_cb); - -static void trigger_sched_softirq (void *data) -{ -raise_softirq_irqoff (SCHED_SOFTIRQ ); -} - -static inline void init_sched_softirq_csd (struct call_single_data *csd) -{ - csd->func = trigger_sched_softirq; - csd->info =NULL ; - csd->flags =0 ; - csd->priv =0 ; -} -/* * idle load balancing details * - One of the idle CPUs nominates itself as idle load_balancer, while
@@ -4450,11 +4434,16 @@ static void nohz_balancer_kick(int cpu) }if (!cpu_rq (ilb_cpu)->nohz_balance_kick) { -struct call_single_data *cp; -cpu_rq (ilb_cpu)->nohz_balance_kick =1 ; - cp = &per_cpu (remote_sched_softirq_cb, cpu); -__smp_call_function_single (ilb_cpu, cp,0 ); + +smp_mb (); +/* +* Use smp_send_reschedule() instead of resched_cpu(). +* This way we generate a sched IPI on the target cpu which +* is idle. And the softirq performing nohz idle load balance +* will be run before returning from the IPI. +*/ +smp_send_reschedule (ilb_cpu); }return ; }
PATCH v2 2/2 sched: Request for idle balance during nohz idle load balance
rq's idle_at_tick is set to idle/busy during the timer tick depending on the cpu was idle or not. This will be used later in the load balance that will be done in the softirq context (which is a process context in -RT kernels).
For nohz kernels, for the cpu doing nohz idle load balance on behalf of all the idle cpu's, its rq->idle_at_tick might have a stale value (which is recorded when it got the timer tick presumably when it is busy).
As the nohz idle load balancing is also being done at the same place as the regular load balancing, nohz idle load balancing was bailing out when it sees rq's idle_at_tick not set.
Thus leading to poor system utilization.
Rename rq's idle_at_tick to idle_balance and set it when someone requests for nohz idle balance on an idle cpu.
--- kernel/sched.c | 8 +++++--- kernel/sched_fair.c | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-)
kernel/sched.c +5 -3
@@ -644,7 +644,7 @@ struct rq {unsigned long cpu_power; -unsigned char idle_at_tick; +unsigned char idle_balance;/* For active balancing */ int post_schedule;int active_balance;
@@ -2751,8 +2751,10 @@ void scheduler_ipi(void)/* * Check if someone kicked us for doing the nohz idle load balance. */ -if (unlikely (got_nohz_idle_kick () && !need_resched ())) +if (unlikely (got_nohz_idle_kick () && !need_resched ())) { +this_rq ()->idle_balance =1 ;raise_softirq_irqoff (SCHED_SOFTIRQ ); + }irq_exit (); }
@@ -4247,7 +4249,7 @@ void scheduler_tick(void)perf_event_task_tick ();#ifdef CONFIG_SMP - rq->idle_at_tick =idle_cpu (cpu); + rq->idle_balance =idle_cpu (cpu);trigger_load_balance (rq, cpu);#endif }
kernel/sched_fair.c +2 -2
@@ -4676,7 +4676,7 @@ static inline int nohz_kick_needed(strucif (time_before (now, nohz.next_balance))return 0 ; -if (rq->idle_at_tick) +if (idle_cpu (cpu))return 0 ; first_pick_cpu =atomic_read (&nohz.first_pick_cpu);
@@ -4712,7 +4712,7 @@ static void run_rebalance_domains(struct {int this_cpu =smp_processor_id ();struct rq *this_rq =cpu_rq (this_cpu); -enum cpu_idle_type idle = this_rq->idle_at_tick ? +enum cpu_idle_type idle = this_rq->idle_balance ?CPU_IDLE :CPU_NOT_IDLE ;rebalance_domains (this_cpu, idle);
kernel/sched_fair.c static void nohz_balancer_kick(int cpu) · line 4446
Quoted 8 lines · Suresh Siddha
kernel/sched_fair.c +8
-0+smp_mb (); +/* +* Use smp_send_reschedule() instead of resched_cpu(). +* This way we generate a sched IPI on the target cpu which +* is idle. And the softirq performing nohz idle load balance +* will be run before returning from the IPI. +*/ +smp_send_reschedule (ilb_cpu);
Now the only thing missing is where the matching barrier is, does receiving an interrupt imply a mb? If so that wants mentioning somewhere, otherwise people will wonder how we can ever read the right value.
Quoted 10 lines · Peter Zijlstra
kernel/sched_fair.c static void nohz_balancer_kick(int cpu) · line 4446
Quoted 8 lines · Suresh Siddha
kernel/sched_fair.c +8
-0+smp_mb (); +/* +* Use smp_send_reschedule() instead of resched_cpu(). +* This way we generate a sched IPI on the target cpu which +* is idle. And the softirq performing nohz idle load balance +* will be run before returning from the IPI. +*/ +smp_send_reschedule (ilb_cpu);
Quoted 4 lines
Now the only thing missing is where the matching barrier is, does receiving an interrupt imply a mb? If so that wants mentioning somewhere, otherwise people will wonder how we can ever read the right value.
This is similar to resched_task()? Isn't it?
I am expecting the interrupt handlers will be already having the necessary memory barriers, if the arch requires it.
thanks,
suresh
On Mon, Oct 03, 2011 at 03:09:00PM -0700, Suresh Siddha wrote:
[..]
Quoted 5 lines
[ This issue is present from 2.6.35+ kernels, but marking it -stable
only from v3.0+ as the proposed fix depends on thescheduler_ipi()that is introduced recently. ]
Hi Suresh,
Are you planning to fix this issue for older kernels too? I am wondering how to go about fixing it there.
Thanks
Vivek
Quoted 8 lines · Vivek Goyal
On Mon, Oct 03, 2011 at 03:09:00PM -0700, Suresh Siddha wrote:
[..]Quoted 5 lines
[ This issue is present from 2.6.35+ kernels, but marking it -stable
only from v3.0+ as the proposed fix depends on thescheduler_ipi()that is introduced recently. ]
Quoted 5 lines
Hi Suresh,
Are you planning to fix this issue for older kernels too? I am wondering how to go about fixing it there.
Vivek, Initially when Prarit brought this issue to me, I gave an ugly quick fix (Appended) for a quick try. But I would recommend we back port the couple of mainline patches that introduced scheduler_ipi() code along with these two patches. Thoughts?
---
Quick and dirty fix for the deadlock caused by the nohz balance logic.
---
kernel/sched_fair.c +2 -1
@@ -3618,6 +3618,7 @@ static DEFINE_PER_CPU(struct call_single_data, remote_sched_softirq_cb);static void trigger_sched_softirq (void *data) {raise_softirq_irqoff (SCHED_SOFTIRQ ); +this_rq ()->nohz_balance_kick =2 ; }static inline void init_sched_softirq_csd (struct call_single_data *csd)
@@ -3977,7 +3978,7 @@ static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)struct rq *rq;int balance_cpu; -if (idle !=CPU_IDLE || !this_rq->nohz_balance_kick) +if (idle !=CPU_IDLE || (this_rq->nohz_balance_kick !=2 ))return ;for_each_cpu (balance_cpu, nohz.idle_cpus_mask) {
Quoted 11 lines · Suresh Siddha
Quoted 10 lines · Vivek Goyal
On Mon, Oct 03, 2011 at 03:09:00PM -0700, Suresh Siddha wrote:
[..]Quoted 5 lines
[ This issue is present from 2.6.35+ kernels, but marking it -stable
only from v3.0+ as the proposed fix depends on thescheduler_ipi()that is introduced recently. ]Hi Suresh,
Quoted 8 lines
Quoted 3 lines
Are you planning to fix this issue for older kernels too? I am wondering how to go about fixing it there.
Vivek, Initially when Prarit brought this issue to me, I gave an ugly quick fix (Appended) for a quick try. But I would recommend we back port the couple of mainline patches that introduced
scheduler_ipi()code along with these two patches. Thoughts?
I agree that backporting scheduler_ipi() patches makes more sense. Browsing through the code looks like we need just one commit which introduced scheduler_ipi().
commit 184748cc50b2dceb8287f9fb657eda48ff8fcfe7
Author: Peter Zijlstra <[email protected]> Date: Tue Apr 5 17:23:39 2011 +0200
sched: Provide scheduler_ipi() callback in response to smp_send_reschedule()
I did partial backport of above to come up with a fix for rhel6 and put your first patch on top of it.
Once your current patches get merged, I can backport above and two of your patches for older kernel is there is interest.
Thanks
Vivek
Quoted 2 lines · Vivek Goyal
I did partial backport of above to come up with a fix for rhel6 and put your first patch on top of it.
You might also want to look at these for PPC64:
23d72bfd8f9f24aa9efafed3586a99f5669c23d7880102e78547c1db158a17e36cf0cdd98e7ad710
Quoted 2 lines
Once your current patches get merged, I can backport above and two of your patches for older kernel is there is interest.
I think they're in current tip/master already.
Quoted 8 lines · Peter Zijlstra
Quoted 2 lines · Vivek Goyal
I did partial backport of above to come up with a fix for rhel6 and put your first patch on top of it.
You might also want to look at these for PPC64:
23d72bfd8f9f24aa9efafed3586a99f5669c23d7
This seems to be just cleanup of the way IPI are handled in ppc64 (mux/demux). I don't understand the details but looks like not a necessary component of the problem at hand.
Quoted 1 line
880102e78547c1db158a17e36cf0cdd98e7ad710
Here Benjamin talks about following.
Manual merge of arch/powerpc/kernel/smp.c and add missing scheduler_ipi() call to arch/powerpc/platforms/cell/interrupt.c
And I can't find any scheduler_ipi() call in current cell/interrupt.c. So either it got moved or something else happend. CCing him to find out.
Thanks
Vivek