[RFC][PATCH 1/5] sched: Always provide p->oncpu

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Peter Zijlstra
Date: Thursday, December 16, 2010 - 7:56 am

Always provide p->oncpu so that we can determine if its on a cpu
without having to lock the rq.

Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
---
 include/linux/sched.h |    2 --
 kernel/sched.c        |   36 ++++++++++++++++++++++++------------
 2 files changed, 24 insertions(+), 14 deletions(-)

Index: linux-2.6/kernel/sched.c
===================================================================
--- linux-2.6.orig/kernel/sched.c
+++ linux-2.6/kernel/sched.c
@@ -850,18 +850,39 @@ static inline int task_current(struct rq
 	return rq->curr == p;
 }
 
-#ifndef __ARCH_WANT_UNLOCKED_CTXSW
 static inline int task_running(struct rq *rq, struct task_struct *p)
 {
+#ifdef CONFIG_SMP
+	return p->oncpu;
+#else
 	return task_current(rq, p);
+#endif
 }
 
+#ifndef __ARCH_WANT_UNLOCKED_CTXSW
 static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
 {
+#ifdef CONFIG_SMP
+	/*
+	 * We can optimise this out completely for !SMP, because the
+	 * SMP rebalancing from interrupt is the only thing that cares
+	 * here.
+	 */
+	next->oncpu = 1;
+#endif
 }
 
 static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
 {
+#ifdef CONFIG_SMP
+	/*
+	 * After ->oncpu is cleared, the task can be moved to a different CPU.
+	 * We must ensure this doesn't happen until the switch is completely
+	 * finished.
+	 */
+	smp_wmb();
+	prev->oncpu = 0;
+#endif
 #ifdef CONFIG_DEBUG_SPINLOCK
 	/* this is a valid case when another task releases the spinlock */
 	rq->lock.owner = current;
@@ -877,15 +898,6 @@ static inline void finish_lock_switch(st
 }
 
 #else /* __ARCH_WANT_UNLOCKED_CTXSW */
-static inline int task_running(struct rq *rq, struct task_struct *p)
-{
-#ifdef CONFIG_SMP
-	return p->oncpu;
-#else
-	return task_current(rq, p);
-#endif
-}
-
 static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
 {
 #ifdef CONFIG_SMP
@@ -2589,7 +2601,7 @@ void sched_fork(struct task_struct *p, i
 	if (likely(sched_info_on()))
 		memset(&p->sched_info, 0, sizeof(p->sched_info));
 #endif
-#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
+#if defined(CONFIG_SMP)
 	p->oncpu = 0;
 #endif
 #ifdef CONFIG_PREEMPT
@@ -5502,7 +5514,7 @@ void __cpuinit init_idle(struct task_str
 	rcu_read_unlock();
 
 	rq->curr = rq->idle = idle;
-#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
+#if defined(CONFIG_SMP)
 	idle->oncpu = 1;
 #endif
 	raw_spin_unlock_irqrestore(&rq->lock, flags);
Index: linux-2.6/include/linux/sched.h
===================================================================
--- linux-2.6.orig/include/linux/sched.h
+++ linux-2.6/include/linux/sched.h
@@ -1198,10 +1198,8 @@ struct task_struct {
 	int lock_depth;		/* BKL lock depth */
 
 #ifdef CONFIG_SMP
-#ifdef __ARCH_WANT_UNLOCKED_CTXSW
 	int oncpu;
 #endif
-#endif
 
 	int prio, static_prio, normal_prio;
 	unsigned int rt_priority;


--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[RFC][PATCH 0/5] Reduce runqueue lock contention -v2, Peter Zijlstra, (Thu Dec 16, 7:56 am)
[RFC][PATCH 1/5] sched: Always provide p->oncpu, Peter Zijlstra, (Thu Dec 16, 7:56 am)
[RFC][PATCH 3/5] sched: Change the ttwu success details, Peter Zijlstra, (Thu Dec 16, 7:56 am)
[RFC][PATCH 4/5] sched: Clean up ttwu stats, Peter Zijlstra, (Thu Dec 16, 7:56 am)
[RFC][PATCH 5/5] sched: Reduce ttwu rq-&gt;lock contention, Peter Zijlstra, (Thu Dec 16, 7:56 am)
Re: [RFC][PATCH 3/5] sched: Change the ttwu success details, Frederic Weisbecker, (Thu Dec 16, 8:23 am)
Re: [RFC][PATCH 5/5] sched: Reduce ttwu rq-&gt;lock contention, Frederic Weisbecker, (Thu Dec 16, 8:31 am)
Re: [RFC][PATCH 3/5] sched: Change the ttwu success details, Frederic Weisbecker, (Thu Dec 16, 8:35 am)
Re: [RFC][PATCH 3/5] sched: Change the ttwu success details, Frederic Weisbecker, (Thu Dec 16, 8:45 am)
Re: [RFC][PATCH 0/5] Reduce runqueue lock contention -v2, Frank Rowand, (Thu Dec 16, 12:12 pm)
Re: [RFC][PATCH 0/5] Reduce runqueue lock contention -v2, Frank Rowand, (Thu Dec 16, 12:36 pm)
Re: [RFC][PATCH 0/5] Reduce runqueue lock contention -v2, Frank Rowand, (Thu Dec 16, 12:36 pm)
Re: [RFC][PATCH 0/5] Reduce runqueue lock contention -v2, Frank Rowand, (Thu Dec 16, 12:39 pm)
Re: [RFC][PATCH 0/5] Reduce runqueue lock contention -v2, Peter Zijlstra, (Thu Dec 16, 12:42 pm)
Re: [RFC][PATCH 1/5] sched: Always provide p-&gt;oncpu, Frank Rowand, (Fri Dec 17, 6:03 pm)
Re: [RFC][PATCH 4/5] sched: Clean up ttwu stats, Frank Rowand, (Fri Dec 17, 6:09 pm)