This is a note to let you know that I've just added the patch titled sched: Fix affine_move_task() self-concurrency to the 5.11-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: sched-fix-affine_move_task-self-concurrency.patch and it can be found in the queue-5.11 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From 9e81889c7648d48dd5fe13f41cbc99f3c362484a Mon Sep 17 00:00:00 2001 From: Peter Zijlstra <peterz@xxxxxxxxxxxxx> Date: Wed, 24 Feb 2021 11:31:09 +0100 Subject: sched: Fix affine_move_task() self-concurrency From: Peter Zijlstra <peterz@xxxxxxxxxxxxx> commit 9e81889c7648d48dd5fe13f41cbc99f3c362484a upstream. 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: 6d337eab041d ("sched: Fix migrate_disable() vs set_cpus_allowed_ptr()") Cc: stable@xxxxxxxxxx Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx> Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx> Reviewed-by: Valentin Schneider <valentin.schneider@xxxxxxx> Link: https://lkml.kernel.org/r/20210224131355.649146419@xxxxxxxxxxxxx Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- kernel/sched/core.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -1864,6 +1864,7 @@ struct migration_arg { struct set_affinity_pending { refcount_t refs; + unsigned int stop_pending; struct completion done; struct cpu_stop_work stop_work; struct migration_arg arg; @@ -1982,12 +1983,15 @@ static int migration_cpu_stop(void *data * determine is_migration_disabled() and so have to chase after * it. */ + WARN_ON_ONCE(!pending->stop_pending); task_rq_unlock(rq, p, &rf); stop_one_cpu_nowait(task_cpu(p), migration_cpu_stop, &pending->arg, &pending->stop_work); return 0; } out: + if (pending) + pending->stop_pending = false; task_rq_unlock(rq, p, &rf); if (complete) @@ -2183,7 +2187,7 @@ static int affine_move_task(struct rq *r int dest_cpu, unsigned int flags) { struct set_affinity_pending my_pending = { }, *pending = NULL; - bool complete = false; + bool stop_pending, complete = false; /* Can the task run on the task's current CPU? If so, we're done */ if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) { @@ -2256,14 +2260,19 @@ static int affine_move_task(struct rq *r * anything else we cannot do is_migration_disabled(), punt * and have the stopper function handle it all race-free. */ + stop_pending = pending->stop_pending; + if (!stop_pending) + pending->stop_pending = true; refcount_inc(&pending->refs); /* pending->{arg,stop_work} */ if (flags & SCA_MIGRATE_ENABLE) p->migration_flags &= ~MDF_PUSH; task_rq_unlock(rq, p, rf); - stop_one_cpu_nowait(cpu_of(rq), migration_cpu_stop, - &pending->arg, &pending->stop_work); + if (!stop_pending) { + stop_one_cpu_nowait(cpu_of(rq), migration_cpu_stop, + &pending->arg, &pending->stop_work); + } if (flags & SCA_MIGRATE_ENABLE) return 0; Patches currently in stable-queue which might be from peterz@xxxxxxxxxxxxx are queue-5.11/powerpc-perf-fix-handling-of-privilege-level-checks-in-perf-interrupt-context.patch queue-5.11/sched-fix-migration_cpu_stop-requeueing.patch queue-5.11/sched-simplify-set_affinity_pending-refcounts.patch queue-5.11/perf-traceevent-ensure-read-cmdlines-are-null-terminated.patch queue-5.11/perf-core-flush-pmu-internal-buffers-for-per-cpu-eve.patch queue-5.11/sched-simplify-migration_cpu_stop.patch queue-5.11/sched-membarrier-fix-missing-local-execution-of-ipi_sync_rq_state.patch queue-5.11/x86-unwind-orc-disable-kasan-checking-in-the-orc-unwinder-part-2.patch queue-5.11/arm64-perf-fix-64-bit-event-counter-read-truncation.patch queue-5.11/sched-collate-affine_move_task-stoppers.patch queue-5.11/sched-fix-affine_move_task-self-concurrency.patch queue-5.11/stop_machine-mark-helpers-__always_inline.patch queue-5.11/seqlock-lockdep-fix-seqcount_latch_init.patch queue-5.11/sched-optimize-migration_cpu_stop.patch queue-5.11/perf-build-fix-ccache-usage-in-cc-when-generating-arch-errno-table.patch queue-5.11/mm-userfaultfd-fix-memory-corruption-due-to-writeprotect.patch queue-5.11/perf-x86-intel-set-perf_attach_sched_cb-for-large-pe.patch