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

From: Sebastian Andrzej Siewior

Date: Thu Dec 03 2026 - 07:11:07 EST


On 2026-12-03 19:25:14 [+0800], Zhen Wang wrote:
> 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.

Yeah.

>
> - That breaks migrate_enable() in constrained contexts. We see it

what is a constrained context?

> 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.

So the migrate_enable() didn't succeed in restoring the mask and the
balanced migrate_enable() with the scheduler does it again. And the BPF
code does migrate_en/dis so…

…
> --- 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;
> }

This does not make it right, it just avoids the problem on enabled. The
mask still does not match the CPU it runs on. do_set_cpus_allowed() was
not yet invoked yet so that is nice.
Do you see the printk where select_fallback_rq() fixes the mask?

Sebastian