Re: [PATCH v3] sched/rt: Do not account migrate-disabled tasks as pushable

From: Peter Zijlstra

Date: Thu Nov 19 2026 - 10:08:59 EST


On Thu, Nov 19, 2026 at 12:06:21PM +0000, Mel Gorman wrote:

> > > > Suppose:
> > > >
> > > > migrate_disable();
> > > > ...
> > > > schedule()
> > > > migrate_disable_switch()
> > > > // switches p->cpus_ptr and p->nr_cpus_allowed
> > > > pick_next_task()
> > > > p = class->pick_task();
> > > > put_prev_set_next_task(rq, rq->donor, p);
> > > > return p;
> > > >
> > > > So any task that ends up in put_prev_task_rt() wlll have passed through
> > > > migrate_disable_switch(), which have updated nr_cpus_allowed.
> > > >
>
> Short summary, this assumption is wrong, nr_cpus_allowed is not updated
> in that path.


> That sounded plausible and while Steven did not explicitly say so, I
> think he agreed with you and it sounded believable to me but looked
> anyway
>
> (disable side)
> migrate_disable_switch
> do_set_cpus_allowed(p, &ac);
> p->sched_class->set_cpus_allowed(p, ctx);
>
> (enable side)
> ___migrate_enable
> __set_cpus_allowed_ptr(p, &ac);
>
> Other than the assymetry, it looks ok so disable side for rt.c
>
> DEFINE_SCHED_CLASS(rt) = {
> .set_cpus_allowed = set_cpus_allowed_common,
> }
>
> Nothing special, back to core.c
>
> void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx)
> {
> if (ctx->flags & (SCA_MIGRATE_ENABLE | SCA_MIGRATE_DISABLE)) {
> p->cpus_ptr = ctx->new_mask;
> return;
> }
>
> cpumask_copy(&p->cpus_mask, ctx->new_mask);
> p->nr_cpus_allowed = cpumask_weight(ctx->new_mask);
> mm_update_cpus_allowed(p->mm, ctx->new_mask);
>
>
> set_cpus_allowed_common only sets cpus_ptr for SCA_MIGRATE_ENABLE|SCA_MIGRATE_DISABLE
> and **nr_cpus_allowed is not changed** which is the assumption the
> straight-forward patch relies on.
>
> I do not recall looking too closely at migrate_disable or forgot why it's
> like this so I kept looking to see where SCA_MIGRATE_ENABLE originated
> (presumably Set Cpus Allowed (SCA) instead of a typo of Ska) leading to
> af449901b84c ("sched: Add migrate_disable()") which was no help explaining
> the why. It's there with a tonne of warnings about locking violations
> but does not explicitly state why. Best is this bit
>
> - if (p->nr_cpus_allowed > 1)
> + if (p->nr_cpus_allowed > 1 && !is_migration_disabled(p))
>
> That sortof suggests the patch relied on the behaviour to distinguish
> between nr_cpus_allowed == 1 and nr_cpus_allowed is equivalent to 1
> because if migrate_disabled even though p->migration_enabled existed. I was
> under time pressure at this point so had to draw the line and stop. It's
> not particularly clear why set_cpus_allowed_common() is like this but
> get_push_task() still checks
>
> if (p->nr_cpus_allowed == 1)
> return NULL;
>
> if (p->migration_disabled)
> return NULL;
>
> It's not clear at all to me why both are needed. I expect if I kept digging
> that I would find something clever but the obvious fix does not work,
> or at least if it does work, it's not very obvious why it's ok and has
> no side-effects.

Bah, and I cannot remember either. Let me have a poke, see if something
bubbles up.