Skip to content

Commit 317f394

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
sched: Move the second half of ttwu() to the remote cpu
Now that we've removed the rq->lock requirement from the first part of ttwu() and can compute placement without holding any rq->lock, ensure we execute the second half of ttwu() on the actual cpu we want the task to run on. This avoids having to take rq->lock and doing the task enqueue remotely, saving lots on cacheline transfers. As measured using: http://oss.oracle.com/~mason/sembench.c $ for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor ; do echo performance > $i; done $ echo 4096 32000 64 128 > /proc/sys/kernel/sem $ ./sembench -t 2048 -w 1900 -o 0 unpatched: run time 30 seconds 647278 worker burns per second patched: run time 30 seconds 816715 worker burns per second Reviewed-by: Frank Rowand <frank.rowand@am.sony.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Nick Piggin <npiggin@kernel.dk> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/20110405152729.515897185@chello.nl
1 parent c05fbaf commit 317f394

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

include/linux/sched.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,7 @@ struct task_struct {
12031203
int lock_depth; /* BKL lock depth */
12041204

12051205
#ifdef CONFIG_SMP
1206+
struct task_struct *wake_entry;
12061207
int on_cpu;
12071208
#endif
12081209
int on_rq;
@@ -2192,7 +2193,7 @@ extern void set_task_comm(struct task_struct *tsk, char *from);
21922193
extern char *get_task_comm(char *to, struct task_struct *tsk);
21932194

21942195
#ifdef CONFIG_SMP
2195-
static inline void scheduler_ipi(void) { }
2196+
void scheduler_ipi(void);
21962197
extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
21972198
#else
21982199
static inline void scheduler_ipi(void) { }

init/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,11 @@ config SCHED_AUTOGROUP
827827
desktop applications. Task group autogeneration is currently based
828828
upon task session.
829829

830+
config SCHED_TTWU_QUEUE
831+
bool
832+
depends on !SPARC32
833+
default y
834+
830835
config MM_OWNER
831836
bool
832837

kernel/sched.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,10 @@ struct rq {
556556
unsigned int ttwu_count;
557557
unsigned int ttwu_local;
558558
#endif
559+
560+
#ifdef CONFIG_SMP
561+
struct task_struct *wake_list;
562+
#endif
559563
};
560564

561565
static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
@@ -2516,10 +2520,61 @@ static int ttwu_remote(struct task_struct *p, int wake_flags)
25162520
return ret;
25172521
}
25182522

2523+
#ifdef CONFIG_SMP
2524+
static void sched_ttwu_pending(void)
2525+
{
2526+
struct rq *rq = this_rq();
2527+
struct task_struct *list = xchg(&rq->wake_list, NULL);
2528+
2529+
if (!list)
2530+
return;
2531+
2532+
raw_spin_lock(&rq->lock);
2533+
2534+
while (list) {
2535+
struct task_struct *p = list;
2536+
list = list->wake_entry;
2537+
ttwu_do_activate(rq, p, 0);
2538+
}
2539+
2540+
raw_spin_unlock(&rq->lock);
2541+
}
2542+
2543+
void scheduler_ipi(void)
2544+
{
2545+
sched_ttwu_pending();
2546+
}
2547+
2548+
static void ttwu_queue_remote(struct task_struct *p, int cpu)
2549+
{
2550+
struct rq *rq = cpu_rq(cpu);
2551+
struct task_struct *next = rq->wake_list;
2552+
2553+
for (;;) {
2554+
struct task_struct *old = next;
2555+
2556+
p->wake_entry = next;
2557+
next = cmpxchg(&rq->wake_list, old, p);
2558+
if (next == old)
2559+
break;
2560+
}
2561+
2562+
if (!next)
2563+
smp_send_reschedule(cpu);
2564+
}
2565+
#endif
2566+
25192567
static void ttwu_queue(struct task_struct *p, int cpu)
25202568
{
25212569
struct rq *rq = cpu_rq(cpu);
25222570

2571+
#if defined(CONFIG_SMP) && defined(CONFIG_SCHED_TTWU_QUEUE)
2572+
if (sched_feat(TTWU_QUEUE) && cpu != smp_processor_id()) {
2573+
ttwu_queue_remote(p, cpu);
2574+
return;
2575+
}
2576+
#endif
2577+
25232578
raw_spin_lock(&rq->lock);
25242579
ttwu_do_activate(rq, p, 0);
25252580
raw_spin_unlock(&rq->lock);
@@ -6331,6 +6386,7 @@ migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
63316386

63326387
#ifdef CONFIG_HOTPLUG_CPU
63336388
case CPU_DYING:
6389+
sched_ttwu_pending();
63346390
/* Update our root-domain */
63356391
raw_spin_lock_irqsave(&rq->lock, flags);
63366392
if (rq->rd) {

kernel/sched_features.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ SCHED_FEAT(OWNER_SPIN, 1)
6464
* Decrement CPU power based on irq activity
6565
*/
6666
SCHED_FEAT(NONIRQ_POWER, 1)
67+
68+
/*
69+
* Queue remote wakeups on the target CPU and process them
70+
* using the scheduler IPI. Reduces rq->lock contention/bounces.
71+
*/
72+
SCHED_FEAT(TTWU_QUEUE, 1)

0 commit comments

Comments
 (0)