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

From: Peter Zijlstra

Date: Thu Dec 03 2026 - 08:05:40 EST


On Thu, Dec 03, 2026 at 02:50:42AM +0100, Karl Mehltretter wrote:
> On Wed, Dec 02, 2026 at 03:42:40PM +0100, Peter Zijlstra wrote:
> > > > 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);
> > > [...]
> > > > - struct rq *dest_rq = __migrate_task(rq, &rf, p, cpu);
> > > > + struct rq *dest_rq = move_queued_task(rq, &rf, p, cpu);
> > >
> > > Don't you still need the is_cpu_allowed() check here? Both push stoppers
> > > do select_fallback_rq() before taking rq->lock.
> >
> > All hold p->pi_lock, and hence IRQs and preemption are disabled, the
> > affinity mask it locked and hotplug will wait for an RCU grace period
> > due to deactivate_cpu or somesuch.
> >
>
> yes, that makes sense for affinity and hotplug.
>
> I'm still wondering about migrate_disable().
>
> Unlike try_to_wake_up(), the push stoppers call select_fallback_rq()
> before taking rq->lock. If p is running on another CPU with migration
> disabled at that point, is_cpu_allowed() only checks cpu_online().
>
> Could p then re-enable migration and move back to rq before
> rq_lock() in sched_non_preferred_cpu_push_stop()?

I am not quite seeing the race; everything is serialized by p->pi_lock,
no?

Draw me a picture?

The below patch boots without burning the machine down.

---
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1933,6 +1933,7 @@ extern int dl_bw_alloc(int cpu, u64 dl_b
extern void dl_bw_free(int cpu, u64 dl_bw);

/* set_cpus_allowed_force() - consider using set_cpus_allowed_ptr() instead */
+extern void __set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask);
extern void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask);

/**
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -4305,7 +4305,7 @@ bool cpuset_cpus_allowed_fallback(struct
rcu_read_lock();
cs_mask = task_cs(tsk)->cpus_allowed;
if (is_in_v2_mode() && cpumask_subset(cs_mask, possible_mask)) {
- set_cpus_allowed_force(tsk, cs_mask);
+ __set_cpus_allowed_force(tsk, cs_mask);
changed = true;
}
rcu_read_unlock();
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -570,8 +570,7 @@ static void __kthread_bind_mask(struct t
return;
}

- scoped_guard (raw_spinlock_irqsave, &p->pi_lock)
- set_cpus_allowed_force(p, mask);
+ set_cpus_allowed_force(p, mask);

/* It's safe because the task is inactive. */
p->flags |= PF_NO_SETAFFINITY;
@@ -864,8 +863,7 @@ int kthread_affine_preferred(struct task
list_add_tail(&kthread->affinity_node, &kthread_affinity_list);
kthread_fetch_affinity(kthread, affinity);

- scoped_guard (raw_spinlock_irqsave, &p->pi_lock)
- set_cpus_allowed_force(p, affinity);
+ set_cpus_allowed_force(p, affinity);

mutex_unlock(&kthread_affinity_lock);
out:
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2627,27 +2627,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
@@ -2670,7 +2650,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();
@@ -2708,19 +2688,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
@@ -2846,7 +2823,7 @@ do_set_cpus_allowed(struct task_struct *
* Used for kthread_bind() and select_fallback_rq(), in both cases the user
* affinity (if any) should be destroyed too.
*/
-void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)
+void __set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)
{
struct affinity_context ac = {
.new_mask = new_mask,
@@ -2858,8 +2835,7 @@ void set_cpus_allowed_force(struct task_
struct rcu_head rcu;
};

- scoped_guard (__task_rq_lock, p)
- do_set_cpus_allowed(p, &ac);
+ do_set_cpus_allowed(p, &ac);

/*
* Because this is called with p->pi_lock held, it is not possible
@@ -2869,6 +2845,12 @@ void set_cpus_allowed_force(struct task_
kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
}

+void set_cpus_allowed_force(struct task_struct *p, const struct cpumask *new_mask)
+{
+ guard(task_rq_lock)(p);
+ __set_cpus_allowed_force(p, new_mask);
+}
+
int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
int node)
{
@@ -3595,7 +3577,7 @@ EXPORT_SYMBOL_GPL(kick_process);
* select_task_rq() below may allow selection of !active CPUs in order
* to satisfy the above rules.
*/
-static int select_fallback_rq(int cpu, struct task_struct *p)
+static int __select_fallback_rq(int cpu, struct task_struct *p)
{
int nid = cpu_to_node(cpu);
const struct cpumask *nodemask = NULL;
@@ -3635,7 +3617,7 @@ static int select_fallback_rq(int cpu, s
}
fallthrough;
case possible:
- set_cpus_allowed_force(p, task_cpu_fallback_mask(p));
+ __set_cpus_allowed_force(p, task_cpu_fallback_mask(p));
state = fail;
break;
case fail:
@@ -3660,6 +3642,12 @@ static int select_fallback_rq(int cpu, s
return dest_cpu;
}

+static int select_fallback_rq(int cpu, struct task_struct *p)
+{
+ guard(__task_rq_lock)(p);
+ return __select_fallback_rq(cpu, p);
+}
+
/*
* The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable.
*/
@@ -8415,7 +8403,7 @@ static int __balance_push_cpu_stop(void
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);
}

@@ -11326,7 +11314,7 @@ static int sched_non_preferred_cpu_push_
update_rq_clock(rq);

if (task_rq(p) == rq && task_on_rq_queued(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);