Skip to content

Commit 7460231

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
sched: Fix race in migrate_swap_stop()
There is a subtle race in migrate_swap, when task P, on CPU A, decides to swap places with task T, on CPU B. Task P: - call migrate_swap Task T: - go to sleep, removing itself from the runqueue Task P: - double lock the runqueues on CPU A & B Task T: - get woken up, place itself on the runqueue of CPU C Task P: - see that task T is on a runqueue, and pretend to remove it from the runqueue on CPU B Now CPUs B & C both have corrupted scheduler data structures. This patch fixes it, by holding the pi_lock for both of the tasks involved in the migrate swap. This prevents task T from waking up, and placing itself onto another runqueue, until after migrate_swap has released all locks. This means that, when migrate_swap checks, task T will be either on the runqueue where it was originally seen, or not on any runqueue at all. Migrate_swap deals correctly with of those cases. Tested-by: Joe Mario <[email protected]> Acked-by: Mel Gorman <[email protected]> Reviewed-by: Rik van Riel <[email protected]> Signed-off-by: Peter Zijlstra <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 7c3f2ab commit 7460231

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

kernel/sched/core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,8 @@ static int migrate_swap_stop(void *data)
10491049
src_rq = cpu_rq(arg->src_cpu);
10501050
dst_rq = cpu_rq(arg->dst_cpu);
10511051

1052+
double_raw_lock(&arg->src_task->pi_lock,
1053+
&arg->dst_task->pi_lock);
10521054
double_rq_lock(src_rq, dst_rq);
10531055
if (task_cpu(arg->dst_task) != arg->dst_cpu)
10541056
goto unlock;
@@ -1069,6 +1071,8 @@ static int migrate_swap_stop(void *data)
10691071

10701072
unlock:
10711073
double_rq_unlock(src_rq, dst_rq);
1074+
raw_spin_unlock(&arg->dst_task->pi_lock);
1075+
raw_spin_unlock(&arg->src_task->pi_lock);
10721076

10731077
return ret;
10741078
}

kernel/sched/fair.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,15 +1448,6 @@ static inline void put_numa_group(struct numa_group *grp)
14481448
kfree_rcu(grp, rcu);
14491449
}
14501450

1451-
static void double_lock(spinlock_t *l1, spinlock_t *l2)
1452-
{
1453-
if (l1 > l2)
1454-
swap(l1, l2);
1455-
1456-
spin_lock(l1);
1457-
spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1458-
}
1459-
14601451
static void task_numa_group(struct task_struct *p, int cpupid, int flags,
14611452
int *priv)
14621453
{

kernel/sched/sched.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,24 @@ static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
12491249
lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
12501250
}
12511251

1252+
static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1253+
{
1254+
if (l1 > l2)
1255+
swap(l1, l2);
1256+
1257+
spin_lock(l1);
1258+
spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1259+
}
1260+
1261+
static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1262+
{
1263+
if (l1 > l2)
1264+
swap(l1, l2);
1265+
1266+
raw_spin_lock(l1);
1267+
raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1268+
}
1269+
12521270
/*
12531271
* double_rq_lock - safely lock two runqueues
12541272
*

0 commit comments

Comments
 (0)