[tip: sched/urgent] sched/core: Fix race between set_cpus_allowed_ptr() and CPU hot-plug

From: tip-bot2 for Peter Zijlstra (Intel)

Date: Fri Dec 04 2026 - 04:19:24 EST


The following commit has been merged into the sched/urgent branch of tip:

Commit-ID: 95266806273dfc1aa0833a8cf716442746cdc954
Gitweb: https://git.kernel.org/tip/95266806273dfc1aa0833a8cf716442746cdc954
Author: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
AuthorDate: Thu, 03 Dec 2026 15:46:08 +01:00
Committer: Ingo Molnar <mingo@xxxxxxxxxx>
CommitterDate: Fri, 04 Dec 2026 09:39:34 +01:00

sched/core: Fix race between set_cpus_allowed_ptr() and CPU hot-plug

Sebastian decoded a syzbot report that can reliably trigger
affine_move_task()'s WARN_ON_ONCE(!pending):

"After the affinity of an IRQ has been changed, the
IRQ-core sets IRQTF_AFFINITY and wakes the relevant
interrupt threads which need to adjust their affinity mask.
The thread will then invoke set_cpus_allowed_ptr() to update
the mask. Based on the new mask a CPU is chosen on which the
thread should run.

It is verified that this CPU is online however there is no
guarantee that this CPU remains online while
affine_move_task() is moving _this_ task. Since the current
task requests the migration the stopper/
migration_cpu_stop() is involved at which point the task
pauses for a while. If the CPU goes offline (or is no longer
cpu_online_mask) then __migrate_task() (due to
is_cpu_allowed() reject) will return the rq of the CPU on
which the task is currently running (which does not match
it's task_struct::cpus_mask).

The aftermath: The IRQ-thread's CPU and task's cpus_mask
do not match. A migrate_disable()-> schedule() will change
task_struct::cpus_ptr ensuring that the following
migrate_enable() will update the task to the requested
affinity mask. At this point, affine_move_task() expects
task_struct::migration_pending set but it is NULL because
noone requested an affinity change while the task was in
migrate-disable section.

There are a few spots where set_cpus_allowed_ptr() does
not use cpus_read_lock(). In the IRQ thread case, there will
be an update following if the CPU goes offline so it is not
really needed to block CPU hotplug during the update."

That is:

CPU-A CPU-B
irq_thread()
irq_thread_check_affinity()
set_cpus_allowed_ptr(B)
__set_cpus_allowed_ptr_locked()
affine_move_task()
stop_one_cpu_nowait(..., migration_cpu_stop);

<cpu-hot-unplug> // bye-bye!

</switch-to: stopper-thread>
migration_cpu_stop()
__migrate_task()
is_cpu_allowed() // false! XXX case
complete()

</switch-to: irq_thread>
// now running with cpus_mask = B, on CPU-A
migrate_disable()

schedule()
migrate_disable_switch()

<cpu-hot-plug> // we're back!

migrate_enable()
__set_cpus_allowed_ptr_locked()
dest_cpu = cpumask_any_and_distribute(, &p->cpus_mask); // B
affine_move_task()
if (cpumask_test_cpu(task_cpu(), &p->cpus_mask)) // false!
if (!(flags & SCA_MIGRATE_ENABLE)) // false; this is in fact migrate_enable
WARN_ON_ONCE(!pending) // BOOM!

Cure this by making migration_cpu_stop() call select_fallback_rq() in
case of !is_cpu_allowed() and force the cpumask to something possible.
This avoids getting into the situation where we run on a CPU outside of
our allowed mask.

Since the return value of select_fallback_rq() must, per construction,
be is_cpu_allowed(), there is no point in checking that again, so remove
the __migrate_task() helper and directly call move_queued_task().

Reported-by: syzbot+6835d5c11145e4f77057@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://lore.kernel.org/all/6a9919ac.94649fcc.25487e.0005.GAE@xxxxxxxxxx/
Debugged-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
Fixes: af449901b84c9 ("sched: Add migrate_disable()")
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
kernel/sched/core.c | 43 ++++++++++---------------------------------
1 file changed, 10 insertions(+), 33 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index e089ccc..54df37f 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2655,27 +2655,7 @@ struct set_affinity_pending {
struct migration_arg arg;
};

-/*
- * Move (not current) task off this CPU, onto the destination CPU. We're doing
- * this because either it can't run here any more (set_cpus_allowed()
- * away from this CPU, or CPU going down), or because we're
- * attempting to rebalance this task on exec (sched_exec).
- *
- * So we race with normal scheduler movements, but that's OK, as long
- * as the task is no longer on this CPU.
- */
-static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf,
- struct task_struct *p, int dest_cpu)
- __must_hold(__rq_lockp(rq))
-{
- /* Affinity changed (again). */
- if (!is_cpu_allowed(p, dest_cpu))
- return rq;
-
- rq = move_queued_task(rq, rf, p, dest_cpu);
-
- return rq;
-}
+static int __select_fallback_rq(int cpu, struct task_struct *p);

/*
* migration_cpu_stop - this will be executed by a high-prio stopper thread
@@ -2698,7 +2678,7 @@ static int migration_cpu_stop(void *data)
local_irq_save(rf.flags);
/*
* We need to explicitly wake pending tasks before running
- * __migrate_task() such that we will not miss enforcing cpus_ptr
+ * move_queued_task() such that we will not miss enforcing cpus_ptr
* during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
*/
flush_smp_call_function_queue();
@@ -2736,19 +2716,16 @@ static int migration_cpu_stop(void *data)
}

if (task_on_rq_queued(p)) {
+ int cpu = arg->dest_cpu;
+
+ if (!is_cpu_allowed(p, cpu))
+ cpu = __select_fallback_rq(rq->cpu, p);
+
update_rq_clock(rq);
- rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
+ rq = move_queued_task(rq, &rf, p, cpu);
} else {
p->wake_cpu = arg->dest_cpu;
}
-
- /*
- * XXX __migrate_task() can fail, at which point we might end
- * up running on a dodgy CPU, AFAICT this can only happen
- * during CPU hotplug, at which point we'll get pushed out
- * anyway, so it's probably not a big deal.
- */
-
} else if (pending) {
/*
* This happens when we get migrated between migrate_enable()'s
@@ -8480,7 +8457,7 @@ static int __balance_push_cpu_stop(void *arg)
rq_lock(rq, &rf);
update_rq_clock(rq);
if (task_rq(p) == rq && task_on_rq_queued(p))
- rq = __migrate_task(rq, &rf, p, cpu);
+ rq = move_queued_task(rq, &rf, p, cpu);
rq_unlock(rq, &rf);
}

@@ -11392,7 +11369,7 @@ static int sched_non_preferred_cpu_push_stop(void *arg)

if (task_rq(p) == rq && task_on_rq_queued(p) &&
!is_migration_disabled(p)) {
- struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
+ struct rq *dest_rq = move_queued_task(rq, &rf, p, cpu);

if (rq != dest_rq)
schedstat_inc(p->stats.nr_migrations_cpu_non_preferred);