Re: [PATCH v6 21/37] sched: Make deadline policy and affinity changes wait for a cpuset attach

From: sashiko-bot

Date: Mon Nov 16 2026 - 14:31:10 EST


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] sched: broken cpuset rollback in __sched_setaffinity() with
SCA_DL_NOWAIT
--

commit 9d195b268d19186fd430b17e56b76c3ac49f0d39
Author: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>

sched: Make deadline policy and affinity changes wait for a cpuset attach

This commit introduces cpuset_lock_no_attach() and SCA_DL_NOWAIT so that
SCHED_DEADLINE policy changes in __sched_setscheduler() and affinity
changes in sched_setaffinity() wait for an in-progress cpuset attach to
finish.

> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 2a964286a7cd0..52c291de384e7 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -3195,6 +3195,16 @@ static int __set_cpus_allowed_ptr_locked(struct task_struct *p,
> goto out;
> }
>
> + /*
> + * sched_setaffinity() tested the policy with no lock, and did not
> + * wait for a cpuset attach in progress. @p became a deadline task
> + * since: let the caller wait and try again.
> + */
> + if ((ctx->flags & SCA_DL_NOWAIT) && task_has_dl_policy(p)) {
> + ret = -EAGAIN;
> + goto out;
> + }
> +
> /*
> * Check under the same lock that __sched_setscheduler() holds for
> * its admission test, so that p cannot become SCHED_DEADLINE
> diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
> index 4f47b17151ed9..70b364b4f82dd 100644
> --- a/kernel/sched/syscalls.c
> +++ b/kernel/sched/syscalls.c

[ ... ]

> @@ -1318,13 +1324,31 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
> return -ENOMEM;
> }
>
> - ac = (struct affinity_context){
> - .new_mask = in_mask,
> - .user_mask = user_mask,
> - .flags = SCA_USER,
> - };
> -
> - retval = __sched_setaffinity(p, &ac);
> + /*
> + * Wait for a cpuset attach in progress before changing the affinity
> + * of a deadline task. cpuset_can_attach() reserved the bandwidth of
> + * the task for the CPUs it moves to, and cpuset_attach() moves it
> + * there. A new affinity in between could change those CPUs.
> + *
> + * The policy is tested here with no lock. A task that becomes a
> + * deadline task after this test is found under its runqueue lock,
> + * with SCA_DL_NOWAIT. Wait and try again then.
> + */
> + for (;;) {
> + dl = task_has_dl_policy(p);
> + ac = (struct affinity_context){
> + .new_mask = in_mask,
> + .user_mask = user_mask,
> + .flags = SCA_USER | (dl ? 0 : SCA_DL_NOWAIT),
> + };
> + if (dl)
> + cpuset_lock_no_attach();
> + retval = __sched_setaffinity(p, &ac);
> + if (dl)
> + cpuset_unlock();
> + if (dl || retval != -EAGAIN)
> + break;
> + }
> kfree(ac.user_mask);
>
> return retval;

[Severity: Medium]
Can SCA_DL_NOWAIT cause the cpuset race rollback in __sched_setaffinity()
to fail and leave p->cpus_mask and p->user_cpus_ptr un-restored?

When p is initially not a deadline task, sched_setaffinity() passes
SCA_DL_NOWAIT without holding cpuset_mutex. If the first
__set_cpus_allowed_ptr() succeeds while a concurrent cpuset update narrows
cpus_allowed and another thread changes p to SCHED_DEADLINE, the recovery
path in __sched_setaffinity() clears SCA_DL_CHECK but leaves SCA_DL_NOWAIT
set:

kernel/sched/syscalls.c:__sched_setaffinity() {
...
cpuset_cpus_allowed(p, cpus_allowed);
if (!cpumask_subset(new_mask, cpus_allowed)) {
...
ctx->flags &= ~SCA_DL_CHECK;
__set_cpus_allowed_ptr(p, ctx);
retval = -EINVAL;
}
...
}

CPU0 CPU1
sched_setaffinity(p)
dl = false (SCA_DL_NOWAIT set)
__sched_setaffinity(p, &ac)
cpuset_cpus_allowed(p, ...)
__set_cpus_allowed_ptr(p, ctx)
sets p->cpus_mask = new_mask
swaps p->user_cpus_ptr
with ctx->user_mask
cpuset narrows p's cpus_allowed
sched_setattr(p, SCHED_DEADLINE)
cpuset_cpus_allowed(p, ...)
!cpumask_subset(new_mask, ...)
ctx->flags &= ~SCA_DL_CHECK
__set_cpus_allowed_ptr(p, ctx)
SCA_DL_NOWAIT && dl_policy
returns -EAGAIN (ignored) <-- rollback skipped
returns -EINVAL
kfree(ac.user_mask)

Because the second __set_cpus_allowed_ptr() aborts with -EAGAIN and its
return value is ignored, p->cpus_mask is left containing CPUs outside the
task's cpuset cpus_allowed, and the rejected user_mask remains installed
in p->user_cpus_ptr while the previous user_cpus_ptr is freed.

Clearing SCA_DL_NOWAIT alongside SCA_DL_CHECK before the recovery
__set_cpus_allowed_ptr() call would allow the rollback to complete.

--
Sashiko AI review · https://sashiko.dev/#/patchset/20261116190549.230119-1-mathieu.desnoyers@xxxxxxxxxxxx?part=21