Re: [PATCH v7 23/40] sched: Make deadline policy and affinity changes wait for a cpuset attach

From: sashiko-bot

Date: Tue Nov 17 2026 - 10:18:17 EST


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] sched: stale cpumask before cpuset_lock_no_attach() in
__sched_setaffinity()
--

commit b29e127976b751a796fee47f2183b160f8e83a4d
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 uses it in
__sched_setscheduler() and sched_setaffinity() so that deadline policy and
affinity changes wait until any in-progress cpuset task attach finishes.

> diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
> index 4f47b17151ed9..4aa3ccbff320f 100644
> --- a/kernel/sched/syscalls.c
> +++ b/kernel/sched/syscalls.c

[ ... ]

> @@ -1271,9 +1276,21 @@ int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx)
> /*
> * The cpuset mask must win here, as it does when
> * cpuset_update_tasks_cpumask() sets it on a deadline task.
> + *
> + * A sched_setattr() may have made @p a deadline task since
> + * the first call. SCA_DL_NOWAIT would then skip this second
> + * call. Wait for a cpuset attach in progress instead, as
> + * sched_setaffinity() does for a deadline task.
> */
> ctx->flags &= ~SCA_DL_CHECK;
> - __set_cpus_allowed_ptr(p, ctx);
> + if (ctx->flags & SCA_DL_NOWAIT) {
> + ctx->flags &= ~SCA_DL_NOWAIT;
> + cpuset_lock_no_attach();
> + __set_cpus_allowed_ptr(p, ctx);
> + cpuset_unlock();
> + } else {
> + __set_cpus_allowed_ptr(p, ctx);
> + }
> retval = -EINVAL;
> }

[Severity: Medium]
Can this second __set_cpus_allowed_ptr() call apply a stale new_mask after
cpuset_lock_no_attach() sleeps?

In __sched_setaffinity(), when the concurrent cpuset update recovery path
(!cpumask_subset(new_mask, cpus_allowed)) is entered with SCA_DL_NOWAIT set,
cpuset_cpus_allowed(p, cpus_allowed) and new_mask are computed before
cpuset_lock_no_attach() is called:

kernel/sched/syscalls.c:__sched_setaffinity() {
...
cpuset_cpus_allowed(p, cpus_allowed);
if (!cpumask_subset(new_mask, cpus_allowed)) {
cpumask_copy(new_mask, cpus_allowed);
if (unlikely((ctx->flags & SCA_USER) && ctx->user_mask)) {
bool empty = !cpumask_and(new_mask, new_mask,
ctx->user_mask);

if (empty)
cpumask_copy(new_mask, cpus_allowed);
}
...
}

If p becomes a SCHED_DEADLINE task after the first __set_cpus_allowed_ptr()
call and a cpuset attach or update runs before cpuset_lock_no_attach()
acquires cpuset_mutex, new_mask is not recomputed under cpuset_mutex:

CPU0 CPU1
sched_setaffinity(p)
!task_has_dl_policy(p)
__sched_setaffinity(p, &ac)
__set_cpus_allowed_ptr()
cpuset_cpus_allowed()
cpumask_copy(new_mask, ...)
sched_setattr(p, SCHED_DEADLINE)
cgroup_procs_write() -> cpuset_attach()
moves p and DL bw to new partition P
cpuset_lock_no_attach()
__set_cpus_allowed_ptr()
sets p->cpus_mask to stale new_mask <-- wrong CPUs for p's DL bw

Because SCA_DL_CHECK is cleared before the second __set_cpus_allowed_ptr()
call, p's affinity is overwritten with the pre-attach cpuset mask while its
deadline bandwidth remains accounted in P's root domain.

In addition, if css_set_move_task() moves p to the destination cpuset before
cpuset_cpus_allowed() is read while attach_ctx.in_progress is still set,
waiting in cpuset_lock_no_attach() delays restoring p->user_cpus_ptr from
ctx->user_mask until after cpuset_attach_task() has already run using the
un-restored p->user_cpus_ptr from the first __set_cpus_allowed_ptr() call,
and then applies new_mask intersected with the old ctx->user_mask without
SCA_DL_CHECK, narrowing the deadline task's affinity below its root domain
span.

Recomputing cpus_allowed and new_mask after acquiring cpuset_lock_no_attach()
(or holding cpuset_lock_no_attach() before reading cpus_allowed on the
recovery path) would ensure the mask matches the post-attach cpuset state.

--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1794922146.git.mathieu.desnoyers@xxxxxxxxxxxx?part=23