Skip to content

Commit 9e81889

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
sched: Fix affine_move_task() self-concurrency
Consider: sched_setaffinity(p, X); sched_setaffinity(p, Y); Then the first will install p->migration_pending = &my_pending; and issue stop_one_cpu_nowait(pending); and the second one will read p->migration_pending and _also_ issue: stop_one_cpu_nowait(pending), the _SAME_ @pending. This causes stopper list corruption. Add set_affinity_pending::stop_pending, to indicate if a stopper is in progress. Fixes: 6d337ea ("sched: Fix migrate_disable() vs set_cpus_allowed_ptr()") Cc: [email protected] Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Valentin Schneider <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 3f1bc11 commit 9e81889

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

kernel/sched/core.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,7 @@ struct migration_arg {
18641864

18651865
struct set_affinity_pending {
18661866
refcount_t refs;
1867+
unsigned int stop_pending;
18671868
struct completion done;
18681869
struct cpu_stop_work stop_work;
18691870
struct migration_arg arg;
@@ -1982,12 +1983,15 @@ static int migration_cpu_stop(void *data)
19821983
* determine is_migration_disabled() and so have to chase after
19831984
* it.
19841985
*/
1986+
WARN_ON_ONCE(!pending->stop_pending);
19851987
task_rq_unlock(rq, p, &rf);
19861988
stop_one_cpu_nowait(task_cpu(p), migration_cpu_stop,
19871989
&pending->arg, &pending->stop_work);
19881990
return 0;
19891991
}
19901992
out:
1993+
if (pending)
1994+
pending->stop_pending = false;
19911995
task_rq_unlock(rq, p, &rf);
19921996

19931997
if (complete)
@@ -2183,7 +2187,7 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag
21832187
int dest_cpu, unsigned int flags)
21842188
{
21852189
struct set_affinity_pending my_pending = { }, *pending = NULL;
2186-
bool complete = false;
2190+
bool stop_pending, complete = false;
21872191

21882192
/* Can the task run on the task's current CPU? If so, we're done */
21892193
if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) {
@@ -2256,14 +2260,19 @@ static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flag
22562260
* anything else we cannot do is_migration_disabled(), punt
22572261
* and have the stopper function handle it all race-free.
22582262
*/
2263+
stop_pending = pending->stop_pending;
2264+
if (!stop_pending)
2265+
pending->stop_pending = true;
22592266

22602267
refcount_inc(&pending->refs); /* pending->{arg,stop_work} */
22612268
if (flags & SCA_MIGRATE_ENABLE)
22622269
p->migration_flags &= ~MDF_PUSH;
22632270
task_rq_unlock(rq, p, rf);
22642271

2265-
stop_one_cpu_nowait(cpu_of(rq), migration_cpu_stop,
2266-
&pending->arg, &pending->stop_work);
2272+
if (!stop_pending) {
2273+
stop_one_cpu_nowait(cpu_of(rq), migration_cpu_stop,
2274+
&pending->arg, &pending->stop_work);
2275+
}
22672276

22682277
if (flags & SCA_MIGRATE_ENABLE)
22692278
return 0;

0 commit comments

Comments
 (0)