Re: [PATCH v3] sched: Reset cpumask if __migrate_task() failed for migration_cpu_stop()

From: Zhen Wang

Date: Thu Dec 03 2026 - 06:26:12 EST


On Thu, Nov 26, 2026 at 11:40:49AM +0100, Peter Zijlstra wrote:
> Fun though; if we did hit this condition then
> migrate_enable() would have silently failed. Also not a good thing.
>
> We should probably have ___migrate_enable() warn on this.

We hit exactly this condition in testing. The difference from the
syzbot trace: the CPU stays offline, so the task's cpus_mask contains
no online CPU at all, and we *do* take the -EINVAL exit even with the
migration_disabled() mitigation switching to cpu_online_mask.

Two extra consequences we see, beyond the silently ignored affinity:

- __migrate_enable() only calls ___migrate_enable() when
p->cpus_ptr != &p->cpus_mask. With a stale cpus_ptr that branch is
taken on every enable: each later migrate_enable() re-enters
__set_cpus_allowed_ptr(), re-takes task_rq_lock() and fails again.
A healthy task returns early without taking any lock.

- That breaks migrate_enable() in constrained contexts. We see it
from a BPF program attached to trace_sched_switch that calls
migrate_enable(): the call takes task_rq_lock() on the runqueue
__schedule() already holds and deadlocks. With an intact cpus_ptr
the enable takes no lock, so the same program is fine.

-- >8 --
From: Zhen Wang <zhen.wang1@xxxxxxxxxx>
Date: Wed, 2 Dec 2026 16:47:03 +0800
Subject: [RFC PATCH] sched/core: Fix cpus_ptr leak leading to spinlock
recursion on CPU hotplug
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When a task changes its affinity to a CPU that is dying,
__set_cpus_allowed_ptr can succeed in updating p->cpus_mask, but
affine_move_task / migration_cpu_stop may fail to migrate the task,
leaving it running on an out-of-affinity CPU.

If the task then enters migrate_disable(), migrate_disable_switch()
sets p->cpus_ptr to the per-CPU cpumask of the current CPU. When
migrate_enable() subsequently calls __set_cpus_allowed_ptr(),
cpumask_any_and_distribute() on p->cpus_mask returns an empty
result because the original target CPU is already offline, causing
__set_cpus_allowed_ptr_locked() to bail out without restoring
p->cpus_ptr back to &p->cpus_mask.

The stale cpus_ptr then causes any later migrate_enable() call
— including those invoked from BPF programs attached to
trace_sched_switch — to enter __set_cpus_allowed_ptr() and
attempt task_rq_lock() on a runqueue already locked by
__schedule(), triggering spinlock recursion.

The comment in __set_cpus_allowed_ptr_locked() already warns
about this:

"Specifically, migration_disabled() tasks must not fail the
cpumask_any_and_distribute() pick below, esp. so on
SCA_MIGRATE_ENABLE, otherwise we'll not call
set_cpus_allowed_common() and actually reset p->cpus_ptr."

However the existing mitigation (using cpu_online_mask for
migration_disabled tasks) is insufficient when the cpumask
contains only offline CPUs.

Fix this by restoring p->cpus_ptr to ctx->new_mask even when
a valid destination CPU cannot be found, when called under
SCA_MIGRATE_ENABLE. This is done under rq->lock and p->pi_lock,
satisfying the locking requirement for cpus_ptr updates.

Fixes: af449901b84c9 ("sched: Add migrate_disable()")
Signed-off-by: Zhen Wang <zhen.wang1@xxxxxxxxxx>
---
kernel/sched/core.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 8f5e15af3c4e..222dad4b73e3 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3245,6 +3245,8 @@ static int __set_cpus_allowed_ptr_locked(struct task_struct *p,
*/
dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, ctx->new_mask);
if (dest_cpu >= nr_cpu_ids) {
+ if (ctx->flags & SCA_MIGRATE_ENABLE)
+ p->cpus_ptr = ctx->new_mask;
ret = -EINVAL;
goto out;
}
--
2.34.1

________________________________
This email (including its attachments) is intended only for the person or entity to which it is addressed and may contain information that is privileged, confidential or otherwise protected from disclosure. Unauthorized use, dissemination, distribution or copying of this email or the information herein or taking any action in reliance on the contents of this email or the information herein, by anyone other than the intended recipient, or an employee or agent responsible for delivering the message to the intended recipient, is strictly prohibited. If you are not the intended recipient, please do not read, copy, use or disclose any part of this e-mail to others. Please notify the sender immediately and permanently delete this e-mail and any attachments if you received it in error. Internet communications cannot be guaranteed to be timely, secure, error-free or virus-free. The sender does not accept liability for any errors or omissions.
本邮件及其附件具有保密性质,受法律保护不得泄露,仅发送给本邮件所指特定收件人。严禁非经授权使用、宣传、发布或复制本邮件或其内容。若非该特定收件人,请勿阅读、复制、 使用或披露本邮件的任何内容。若误收本邮件,请从系统中永久性删除本邮件及所有附件,并以回复邮件的方式即刻告知发件人。无法保证互联网通信及时、安全、无误或防毒。发件人对任何错漏均不承担责任。