[PATCH 2/3] sched: Move idle_balance() to post_schedule

From: Steven Rostedt
Date: Tue Feb 12 2013 - 18:01:01 EST

From: "Steven Rostedt (Red Hat)" <rostedt@xxxxxxxxxxx>

The idle_balance() code is called to do task load balancing just before
going to idle. This makes sense as the CPU is about to sleep anyway.
But currently it's called in the middle of the scheduler and in a place
that must have interrupts disabled. That means, while the load balancing
is going on, if a task wakes up on this CPU, it wont get to run while
the interrupts are disabled. The idle task doing the balancing will be
clueless about it.

There's no real reason that the idle_balance() needs to be called in the
middle of schedule anyway. The only benefit is that if a task is pulled
to this CPU, it can be scheduled without the need to schedule the idle
task. But load balancing and migrating the task makes a switch to idle
and back negligible.

By using the post_schedule function pointer of the sched class, the
unlikely branch in the hot path of the scheduler can be removed, and
the idle task itself can do the load balancing.

Another advantage of this, is that by moving the idle_balance to the
post_schedule routine, interrupts can now be enabled in the load balance
allowing for interrupts and wakeups to still occur on that CPU while a
balance is taking place. The enabling of interrupts will come as a separate

Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Signed-off-by: Steven Rostedt <rostedt@xxxxxxxxxxx>
kernel/sched/core.c | 3 ---
kernel/sched/idle_task.c | 10 ++++++++++
2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 1dff78a..a9317b7 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2927,9 +2927,6 @@ need_resched:

pre_schedule(rq, prev);

- if (unlikely(!rq->nr_running))
- idle_balance(cpu, rq);
put_prev_task(rq, prev);
next = pick_next_task(rq);
diff --git a/kernel/sched/idle_task.c b/kernel/sched/idle_task.c
index b6baf37..66b5220 100644
--- a/kernel/sched/idle_task.c
+++ b/kernel/sched/idle_task.c
@@ -13,6 +13,11 @@ select_task_rq_idle(struct task_struct *p, int sd_flag, int flags)
return task_cpu(p); /* IDLE tasks as never migrated */
+static void post_schedule_idle(struct rq *rq)
+ idle_balance(smp_processor_id(), rq);
#endif /* CONFIG_SMP */
* Idle tasks are unconditionally rescheduled:
@@ -25,6 +30,10 @@ static void check_preempt_curr_idle(struct rq *rq, struct task_struct *p, int fl
static struct task_struct *pick_next_task_idle(struct rq *rq)
schedstat_inc(rq, sched_goidle);
+#ifdef CONFIG_SMP
+ /* Trigger the post schedule to do an idle_balance */
+ rq->post_schedule = 1;
return rq->idle;

@@ -86,6 +95,7 @@ const struct sched_class idle_sched_class = {

.select_task_rq = select_task_rq_idle,
+ .post_schedule = post_schedule_idle,

.set_curr_task = set_curr_task_idle,

Attachment: signature.asc
Description: This is a digitally signed message part