Commit 3f029d3c authored by Gregory Haskins's avatar Gregory Haskins Committed by Ingo Molnar

sched: Enhance the pre/post scheduling logic

We currently have an explicit "needs_post" vtable method which
returns a stack variable for whether we should later run
post-schedule.  This leads to an awkward exchange of the
variable as it bubbles back up out of the context switch. Peter
Zijlstra observed that this information could be stored in the
run-queue itself instead of handled on the stack.

Therefore, we revert to the method of having context_switch
return void, and update an internal rq->post_schedule variable
when we require further processing.

In addition, we fix a race condition where we try to access
current->sched_class without holding the rq->lock.  This is
technically racy, as the sched-class could change out from
under us.  Instead, we reference the per-rq post_schedule
variable with the runqueue unlocked, but with preemption
disabled to see if we need to reacquire the rq->lock.

Finally, we clean the code up slightly by removing the #ifdef
CONFIG_SMP conditionals from the schedule() call, and implement
some inline helper functions instead.

This patch passes checkpatch, and rt-migrate.
Signed-off-by: default avatarGregory Haskins <ghaskins@novell.com>
Signed-off-by: default avatarPeter Zijlstra <a.p.zijlstra@chello.nl>
LKML-Reference: <20090729150422.17691.55590.stgit@dev.haskins.net>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent c3a2ae3d
...@@ -1047,7 +1047,6 @@ struct sched_class { ...@@ -1047,7 +1047,6 @@ struct sched_class {
struct rq *busiest, struct sched_domain *sd, struct rq *busiest, struct sched_domain *sd,
enum cpu_idle_type idle); enum cpu_idle_type idle);
void (*pre_schedule) (struct rq *this_rq, struct task_struct *task); void (*pre_schedule) (struct rq *this_rq, struct task_struct *task);
int (*needs_post_schedule) (struct rq *this_rq);
void (*post_schedule) (struct rq *this_rq); void (*post_schedule) (struct rq *this_rq);
void (*task_wake_up) (struct rq *this_rq, struct task_struct *task); void (*task_wake_up) (struct rq *this_rq, struct task_struct *task);
......
...@@ -616,6 +616,7 @@ struct rq { ...@@ -616,6 +616,7 @@ struct rq {
unsigned char idle_at_tick; unsigned char idle_at_tick;
/* For active balancing */ /* For active balancing */
int post_schedule;
int active_balance; int active_balance;
int push_cpu; int push_cpu;
/* cpu of this runqueue: */ /* cpu of this runqueue: */
...@@ -2839,17 +2840,11 @@ prepare_task_switch(struct rq *rq, struct task_struct *prev, ...@@ -2839,17 +2840,11 @@ prepare_task_switch(struct rq *rq, struct task_struct *prev,
* with the lock held can cause deadlocks; see schedule() for * with the lock held can cause deadlocks; see schedule() for
* details.) * details.)
*/ */
static int finish_task_switch(struct rq *rq, struct task_struct *prev) static void finish_task_switch(struct rq *rq, struct task_struct *prev)
__releases(rq->lock) __releases(rq->lock)
{ {
struct mm_struct *mm = rq->prev_mm; struct mm_struct *mm = rq->prev_mm;
long prev_state; long prev_state;
int post_schedule = 0;
#ifdef CONFIG_SMP
if (current->sched_class->needs_post_schedule)
post_schedule = current->sched_class->needs_post_schedule(rq);
#endif
rq->prev_mm = NULL; rq->prev_mm = NULL;
...@@ -2880,10 +2875,44 @@ static int finish_task_switch(struct rq *rq, struct task_struct *prev) ...@@ -2880,10 +2875,44 @@ static int finish_task_switch(struct rq *rq, struct task_struct *prev)
kprobe_flush_task(prev); kprobe_flush_task(prev);
put_task_struct(prev); put_task_struct(prev);
} }
}
#ifdef CONFIG_SMP
/* assumes rq->lock is held */
static inline void pre_schedule(struct rq *rq, struct task_struct *prev)
{
if (prev->sched_class->pre_schedule)
prev->sched_class->pre_schedule(rq, prev);
}
/* rq->lock is NOT held, but preemption is disabled */
static inline void post_schedule(struct rq *rq)
{
if (rq->post_schedule) {
unsigned long flags;
spin_lock_irqsave(&rq->lock, flags);
if (rq->curr->sched_class->post_schedule)
rq->curr->sched_class->post_schedule(rq);
spin_unlock_irqrestore(&rq->lock, flags);
rq->post_schedule = 0;
}
}
#else
return post_schedule; static inline void pre_schedule(struct rq *rq, struct task_struct *p)
{
}
static inline void post_schedule(struct rq *rq)
{
} }
#endif
/** /**
* schedule_tail - first thing a freshly forked thread must call. * schedule_tail - first thing a freshly forked thread must call.
* @prev: the thread we just switched away from. * @prev: the thread we just switched away from.
...@@ -2892,14 +2921,14 @@ asmlinkage void schedule_tail(struct task_struct *prev) ...@@ -2892,14 +2921,14 @@ asmlinkage void schedule_tail(struct task_struct *prev)
__releases(rq->lock) __releases(rq->lock)
{ {
struct rq *rq = this_rq(); struct rq *rq = this_rq();
int post_schedule;
post_schedule = finish_task_switch(rq, prev); finish_task_switch(rq, prev);
#ifdef CONFIG_SMP /*
if (post_schedule) * FIXME: do we need to worry about rq being invalidated by the
current->sched_class->post_schedule(rq); * task_switch?
#endif */
post_schedule(rq);
#ifdef __ARCH_WANT_UNLOCKED_CTXSW #ifdef __ARCH_WANT_UNLOCKED_CTXSW
/* In this case, finish_task_switch does not reenable preemption */ /* In this case, finish_task_switch does not reenable preemption */
...@@ -2913,7 +2942,7 @@ asmlinkage void schedule_tail(struct task_struct *prev) ...@@ -2913,7 +2942,7 @@ asmlinkage void schedule_tail(struct task_struct *prev)
* context_switch - switch to the new MM and the new * context_switch - switch to the new MM and the new
* thread's register state. * thread's register state.
*/ */
static inline int static inline void
context_switch(struct rq *rq, struct task_struct *prev, context_switch(struct rq *rq, struct task_struct *prev,
struct task_struct *next) struct task_struct *next)
{ {
...@@ -2960,7 +2989,7 @@ context_switch(struct rq *rq, struct task_struct *prev, ...@@ -2960,7 +2989,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
* CPUs since it called schedule(), thus the 'rq' on its stack * CPUs since it called schedule(), thus the 'rq' on its stack
* frame will be invalid. * frame will be invalid.
*/ */
return finish_task_switch(this_rq(), prev); finish_task_switch(this_rq(), prev);
} }
/* /*
...@@ -5371,7 +5400,6 @@ asmlinkage void __sched schedule(void) ...@@ -5371,7 +5400,6 @@ asmlinkage void __sched schedule(void)
{ {
struct task_struct *prev, *next; struct task_struct *prev, *next;
unsigned long *switch_count; unsigned long *switch_count;
int post_schedule = 0;
struct rq *rq; struct rq *rq;
int cpu; int cpu;
...@@ -5403,10 +5431,7 @@ need_resched_nonpreemptible: ...@@ -5403,10 +5431,7 @@ need_resched_nonpreemptible:
switch_count = &prev->nvcsw; switch_count = &prev->nvcsw;
} }
#ifdef CONFIG_SMP pre_schedule(rq, prev);
if (prev->sched_class->pre_schedule)
prev->sched_class->pre_schedule(rq, prev);
#endif
if (unlikely(!rq->nr_running)) if (unlikely(!rq->nr_running))
idle_balance(cpu, rq); idle_balance(cpu, rq);
...@@ -5422,25 +5447,17 @@ need_resched_nonpreemptible: ...@@ -5422,25 +5447,17 @@ need_resched_nonpreemptible:
rq->curr = next; rq->curr = next;
++*switch_count; ++*switch_count;
post_schedule = context_switch(rq, prev, next); /* unlocks the rq */ context_switch(rq, prev, next); /* unlocks the rq */
/* /*
* the context switch might have flipped the stack from under * the context switch might have flipped the stack from under
* us, hence refresh the local variables. * us, hence refresh the local variables.
*/ */
cpu = smp_processor_id(); cpu = smp_processor_id();
rq = cpu_rq(cpu); rq = cpu_rq(cpu);
} else { } else
#ifdef CONFIG_SMP
if (current->sched_class->needs_post_schedule)
post_schedule = current->sched_class->needs_post_schedule(rq);
#endif
spin_unlock_irq(&rq->lock); spin_unlock_irq(&rq->lock);
}
#ifdef CONFIG_SMP post_schedule(rq);
if (post_schedule)
current->sched_class->post_schedule(rq);
#endif
if (unlikely(reacquire_kernel_lock(current) < 0)) if (unlikely(reacquire_kernel_lock(current) < 0))
goto need_resched_nonpreemptible; goto need_resched_nonpreemptible;
...@@ -9403,6 +9420,7 @@ void __init sched_init(void) ...@@ -9403,6 +9420,7 @@ void __init sched_init(void)
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
rq->sd = NULL; rq->sd = NULL;
rq->rd = NULL; rq->rd = NULL;
rq->post_schedule = 0;
rq->active_balance = 0; rq->active_balance = 0;
rq->next_balance = jiffies; rq->next_balance = jiffies;
rq->push_cpu = 0; rq->push_cpu = 0;
......
...@@ -1056,6 +1056,11 @@ static struct task_struct *_pick_next_task_rt(struct rq *rq) ...@@ -1056,6 +1056,11 @@ static struct task_struct *_pick_next_task_rt(struct rq *rq)
return p; return p;
} }
static inline int has_pushable_tasks(struct rq *rq)
{
return !plist_head_empty(&rq->rt.pushable_tasks);
}
static struct task_struct *pick_next_task_rt(struct rq *rq) static struct task_struct *pick_next_task_rt(struct rq *rq)
{ {
struct task_struct *p = _pick_next_task_rt(rq); struct task_struct *p = _pick_next_task_rt(rq);
...@@ -1064,6 +1069,12 @@ static struct task_struct *pick_next_task_rt(struct rq *rq) ...@@ -1064,6 +1069,12 @@ static struct task_struct *pick_next_task_rt(struct rq *rq)
if (p) if (p)
dequeue_pushable_task(rq, p); dequeue_pushable_task(rq, p);
/*
* We detect this state here so that we can avoid taking the RQ
* lock again later if there is no need to push
*/
rq->post_schedule = has_pushable_tasks(rq);
return p; return p;
} }
...@@ -1262,11 +1273,6 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq) ...@@ -1262,11 +1273,6 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
return lowest_rq; return lowest_rq;
} }
static inline int has_pushable_tasks(struct rq *rq)
{
return !plist_head_empty(&rq->rt.pushable_tasks);
}
static struct task_struct *pick_next_pushable_task(struct rq *rq) static struct task_struct *pick_next_pushable_task(struct rq *rq)
{ {
struct task_struct *p; struct task_struct *p;
...@@ -1466,23 +1472,9 @@ static void pre_schedule_rt(struct rq *rq, struct task_struct *prev) ...@@ -1466,23 +1472,9 @@ static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
pull_rt_task(rq); pull_rt_task(rq);
} }
/*
* assumes rq->lock is held
*/
static int needs_post_schedule_rt(struct rq *rq)
{
return has_pushable_tasks(rq);
}
static void post_schedule_rt(struct rq *rq) static void post_schedule_rt(struct rq *rq)
{ {
/*
* This is only called if needs_post_schedule_rt() indicates that
* we need to push tasks away
*/
spin_lock_irq(&rq->lock);
push_rt_tasks(rq); push_rt_tasks(rq);
spin_unlock_irq(&rq->lock);
} }
/* /*
...@@ -1758,7 +1750,6 @@ static const struct sched_class rt_sched_class = { ...@@ -1758,7 +1750,6 @@ static const struct sched_class rt_sched_class = {
.rq_online = rq_online_rt, .rq_online = rq_online_rt,
.rq_offline = rq_offline_rt, .rq_offline = rq_offline_rt,
.pre_schedule = pre_schedule_rt, .pre_schedule = pre_schedule_rt,
.needs_post_schedule = needs_post_schedule_rt,
.post_schedule = post_schedule_rt, .post_schedule = post_schedule_rt,
.task_wake_up = task_wake_up_rt, .task_wake_up = task_wake_up_rt,
.switched_from = switched_from_rt, .switched_from = switched_from_rt,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment