Skip to content

Commit ac66f54

Browse files
Peter Zijlstraingomolnar
authored andcommitted
sched/numa: Introduce migrate_swap()
Use the new stop_two_cpus() to implement migrate_swap(), a function that flips two tasks between their respective cpus. I'm fairly sure there's a less crude way than employing the stop_two_cpus() method, but everything I tried either got horribly fragile and/or complex. So keep it simple for now. The notable detail is how we 'migrate' tasks that aren't runnable anymore. We'll make it appear like we migrated them before they went to sleep. The sole difference is the previous cpu in the wakeup path, so we override this. Signed-off-by: Peter Zijlstra <peterz@infradead.org> Reviewed-by: Rik van Riel <riel@redhat.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Signed-off-by: Mel Gorman <mgorman@suse.de> Link: http://lkml.kernel.org/r/1381141781-10992-39-git-send-email-mgorman@suse.de Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 1be0bd7 commit ac66f54

7 files changed

Lines changed: 110 additions & 14 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,8 @@ struct task_struct {
10431043
struct task_struct *last_wakee;
10441044
unsigned long wakee_flips;
10451045
unsigned long wakee_flip_decay_ts;
1046+
1047+
int wake_cpu;
10461048
#endif
10471049
int on_rq;
10481050

‎kernel/sched/core.c‎

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,102 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
10131013
__set_task_cpu(p, new_cpu);
10141014
}
10151015

1016+
static void __migrate_swap_task(struct task_struct *p, int cpu)
1017+
{
1018+
if (p->on_rq) {
1019+
struct rq *src_rq, *dst_rq;
1020+
1021+
src_rq = task_rq(p);
1022+
dst_rq = cpu_rq(cpu);
1023+
1024+
deactivate_task(src_rq, p, 0);
1025+
set_task_cpu(p, cpu);
1026+
activate_task(dst_rq, p, 0);
1027+
check_preempt_curr(dst_rq, p, 0);
1028+
} else {
1029+
/*
1030+
* Task isn't running anymore; make it appear like we migrated
1031+
* it before it went to sleep. This means on wakeup we make the
1032+
* previous cpu our targer instead of where it really is.
1033+
*/
1034+
p->wake_cpu = cpu;
1035+
}
1036+
}
1037+
1038+
struct migration_swap_arg {
1039+
struct task_struct *src_task, *dst_task;
1040+
int src_cpu, dst_cpu;
1041+
};
1042+
1043+
static int migrate_swap_stop(void *data)
1044+
{
1045+
struct migration_swap_arg *arg = data;
1046+
struct rq *src_rq, *dst_rq;
1047+
int ret = -EAGAIN;
1048+
1049+
src_rq = cpu_rq(arg->src_cpu);
1050+
dst_rq = cpu_rq(arg->dst_cpu);
1051+
1052+
double_rq_lock(src_rq, dst_rq);
1053+
if (task_cpu(arg->dst_task) != arg->dst_cpu)
1054+
goto unlock;
1055+
1056+
if (task_cpu(arg->src_task) != arg->src_cpu)
1057+
goto unlock;
1058+
1059+
if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task)))
1060+
goto unlock;
1061+
1062+
if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task)))
1063+
goto unlock;
1064+
1065+
__migrate_swap_task(arg->src_task, arg->dst_cpu);
1066+
__migrate_swap_task(arg->dst_task, arg->src_cpu);
1067+
1068+
ret = 0;
1069+
1070+
unlock:
1071+
double_rq_unlock(src_rq, dst_rq);
1072+
1073+
return ret;
1074+
}
1075+
1076+
/*
1077+
* Cross migrate two tasks
1078+
*/
1079+
int migrate_swap(struct task_struct *cur, struct task_struct *p)
1080+
{
1081+
struct migration_swap_arg arg;
1082+
int ret = -EINVAL;
1083+
1084+
get_online_cpus();
1085+
1086+
arg = (struct migration_swap_arg){
1087+
.src_task = cur,
1088+
.src_cpu = task_cpu(cur),
1089+
.dst_task = p,
1090+
.dst_cpu = task_cpu(p),
1091+
};
1092+
1093+
if (arg.src_cpu == arg.dst_cpu)
1094+
goto out;
1095+
1096+
if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
1097+
goto out;
1098+
1099+
if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task)))
1100+
goto out;
1101+
1102+
if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task)))
1103+
goto out;
1104+
1105+
ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
1106+
1107+
out:
1108+
put_online_cpus();
1109+
return ret;
1110+
}
1111+
10161112
struct migration_arg {
10171113
struct task_struct *task;
10181114
int dest_cpu;
@@ -1232,9 +1328,9 @@ static int select_fallback_rq(int cpu, struct task_struct *p)
12321328
* The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
12331329
*/
12341330
static inline
1235-
int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
1331+
int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
12361332
{
1237-
int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags);
1333+
cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
12381334

12391335
/*
12401336
* In order not to call set_task_cpu() on a blocking task we need
@@ -1518,7 +1614,7 @@ try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
15181614
if (p->sched_class->task_waking)
15191615
p->sched_class->task_waking(p);
15201616

1521-
cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
1617+
cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
15221618
if (task_cpu(p) != cpu) {
15231619
wake_flags |= WF_MIGRATED;
15241620
set_task_cpu(p, cpu);
@@ -1752,7 +1848,7 @@ void wake_up_new_task(struct task_struct *p)
17521848
* - cpus_allowed can change in the fork path
17531849
* - any previously selected cpu might disappear through hotplug
17541850
*/
1755-
set_task_cpu(p, select_task_rq(p, SD_BALANCE_FORK, 0));
1851+
set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
17561852
#endif
17571853

17581854
/* Initialize new task's runnable average */
@@ -2080,7 +2176,7 @@ void sched_exec(void)
20802176
int dest_cpu;
20812177

20822178
raw_spin_lock_irqsave(&p->pi_lock, flags);
2083-
dest_cpu = p->sched_class->select_task_rq(p, SD_BALANCE_EXEC, 0);
2179+
dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
20842180
if (dest_cpu == smp_processor_id())
20852181
goto unlock;
20862182

‎kernel/sched/fair.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3706,11 +3706,10 @@ static int select_idle_sibling(struct task_struct *p, int target)
37063706
* preempt must be disabled.
37073707
*/
37083708
static int
3709-
select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
3709+
select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags)
37103710
{
37113711
struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
37123712
int cpu = smp_processor_id();
3713-
int prev_cpu = task_cpu(p);
37143713
int new_cpu = cpu;
37153714
int want_affine = 0;
37163715
int sync = wake_flags & WF_SYNC;

‎kernel/sched/idle_task.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#ifdef CONFIG_SMP
1111
static int
12-
select_task_rq_idle(struct task_struct *p, int sd_flag, int flags)
12+
select_task_rq_idle(struct task_struct *p, int cpu, int sd_flag, int flags)
1313
{
1414
return task_cpu(p); /* IDLE tasks as never migrated */
1515
}

‎kernel/sched/rt.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,13 +1169,10 @@ static void yield_task_rt(struct rq *rq)
11691169
static int find_lowest_rq(struct task_struct *task);
11701170

11711171
static int
1172-
select_task_rq_rt(struct task_struct *p, int sd_flag, int flags)
1172+
select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
11731173
{
11741174
struct task_struct *curr;
11751175
struct rq *rq;
1176-
int cpu;
1177-
1178-
cpu = task_cpu(p);
11791176

11801177
if (p->nr_cpus_allowed == 1)
11811178
goto out;

‎kernel/sched/sched.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ static inline u64 rq_clock_task(struct rq *rq)
558558

559559
#ifdef CONFIG_NUMA_BALANCING
560560
extern int migrate_task_to(struct task_struct *p, int cpu);
561+
extern int migrate_swap(struct task_struct *, struct task_struct *);
561562
static inline void task_numa_free(struct task_struct *p)
562563
{
563564
kfree(p->numa_faults);
@@ -736,6 +737,7 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
736737
*/
737738
smp_wmb();
738739
task_thread_info(p)->cpu = cpu;
740+
p->wake_cpu = cpu;
739741
#endif
740742
}
741743

@@ -991,7 +993,7 @@ struct sched_class {
991993
void (*put_prev_task) (struct rq *rq, struct task_struct *p);
992994

993995
#ifdef CONFIG_SMP
994-
int (*select_task_rq)(struct task_struct *p, int sd_flag, int flags);
996+
int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
995997
void (*migrate_task_rq)(struct task_struct *p, int next_cpu);
996998

997999
void (*pre_schedule) (struct rq *this_rq, struct task_struct *task);

‎kernel/sched/stop_task.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#ifdef CONFIG_SMP
1313
static int
14-
select_task_rq_stop(struct task_struct *p, int sd_flag, int flags)
14+
select_task_rq_stop(struct task_struct *p, int cpu, int sd_flag, int flags)
1515
{
1616
return task_cpu(p); /* stop tasks as never migrate */
1717
}

0 commit comments

Comments
 (0)