[PATCH v6 18/37] sched/deadline: Check SCHED_DEADLINE affinity under the rq lock

From: Mathieu Desnoyers

Date: Mon Nov 16 2026 - 14:07:37 EST


From: Andrea Parri <parri.andrea@xxxxxxxxx>

A task can end up SCHED_DEADLINE with an affinity narrower than its root
domain when sched_setaffinity() and sched_setattr() race on it. Both
calls succeed, although each fails when made after the other:
sched_setattr() with -EPERM, sched_setaffinity() with -EBUSY.

__sched_setaffinity() calls dl_task_check_affinity() before
__set_cpus_allowed_ptr() takes task_rq_lock(), whereas
__sched_setscheduler() runs its admission test and switches the policy
under a single task_rq_lock(). So both calls can pass their checks:

CPU 1: sched_setaffinity(p, cpu0) CPU 2: sched_setattr(p, DEADLINE)

dl_task_check_affinity()
!task_has_dl_policy(p): return 0
task_rq_lock()
admission test passes
p->policy = SCHED_DEADLINE
task_rq_unlock()
__set_cpus_allowed_ptr()
task_rq_lock()
p->cpus_mask = cpu0

The window includes CPU 1 spinning on task_rq_lock() while CPU 2 holds
it. With one thread narrowing a sleeping task to CPU 0 while another
makes it SCHED_DEADLINE, 923514, 959547 and 983997 of 1000000 rounds
ended with both calls succeeding on three boots of a 4-CPU guest.

Move the check into __set_cpus_allowed_ptr_locked(), next to the
PF_NO_SETAFFINITY re-check, under the same lock as the admission test.
Whichever call takes task_rq_lock() second then sees the other's
result: sched_setaffinity() fails with -EBUSY if p is already
SCHED_DEADLINE, and sched_setattr() fails with -EPERM if p's affinity
is already cpu0.

A new SCA_DL_CHECK flag selects the check, and only
__sched_setaffinity() sets it, so sched_setaffinity() and
relax_compatible_cpus_allowed_ptr() keep the check and other callers
stay unchecked. restrict_cpus_allowed_ptr() already does its deadline
check this way.

__sched_setaffinity() clears SCA_DL_CHECK before the second
__set_cpus_allowed_ptr() call it makes after racing with a cpuset
update, so the cpuset mask still wins there, as it does when
cpuset_update_tasks_cpumask() narrows a deadline task. SCA_CHECK stays
set for the PF_NO_SETAFFINITY re-check.

With this applied, the same test ends with both calls succeeding in 0
of 1000000 rounds on each of three boots, and sched_setaffinity()
fails with -EBUSY whenever it loses.

The race was reported by Sebastian. The fix was written with the help
of an LLM.

Fixes: 332ac17ef5bf ("sched/deadline: Add bandwidth management for SCHED_DEADLINE tasks")
Reported-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
Closes: https://lore.kernel.org/20261103081149.DuyWNhVe@xxxxxxxxxxxxx
Signed-off-by: Andrea Parri <parri.andrea@xxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
---
kernel/sched/core.c | 11 +++++++++++
kernel/sched/sched.h | 1 +
kernel/sched/syscalls.c | 12 +++++++-----
3 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index a22df8b43a1a..c77cae5d1be1 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3195,6 +3195,17 @@ static int __set_cpus_allowed_ptr_locked(struct task_struct *p,
goto out;
}

+ /*
+ * Check under the same lock that __sched_setscheduler() holds for
+ * its admission test, so that p cannot become SCHED_DEADLINE
+ * between this check and the mask update below.
+ */
+ if (ctx->flags & SCA_DL_CHECK) {
+ ret = dl_task_check_affinity(p, ctx->new_mask);
+ if (ret)
+ goto out;
+ }
+
if (!(ctx->flags & SCA_MIGRATE_ENABLE)) {
if (cpumask_equal(&p->cpus_mask, ctx->new_mask)) {
if (ctx->flags & SCA_USER)
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index d87296abec4c..fd6d36b6b1e2 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -2982,6 +2982,7 @@ extern struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf);
#define SCA_MIGRATE_DISABLE 0x02
#define SCA_MIGRATE_ENABLE 0x04
#define SCA_USER 0x08
+#define SCA_DL_CHECK 0x10

extern void update_group_capacity(struct sched_domain *sd, int cpu);

diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
index 306cd8d32173..4f47b17151ed 100644
--- a/kernel/sched/syscalls.c
+++ b/kernel/sched/syscalls.c
@@ -1238,11 +1238,7 @@ int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx)
cpumask_and(new_mask, ctx->new_mask, cpus_allowed);

ctx->new_mask = new_mask;
- ctx->flags |= SCA_CHECK;
-
- retval = dl_task_check_affinity(p, new_mask);
- if (retval)
- goto out_free_new_mask;
+ ctx->flags |= SCA_CHECK | SCA_DL_CHECK;

retval = __set_cpus_allowed_ptr(p, ctx);
if (retval)
@@ -1271,6 +1267,12 @@ int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx)
if (empty)
cpumask_copy(new_mask, cpus_allowed);
}
+
+ /*
+ * The cpuset mask must win here, as it does when
+ * cpuset_update_tasks_cpumask() sets it on a deadline task.
+ */
+ ctx->flags &= ~SCA_DL_CHECK;
__set_cpus_allowed_ptr(p, ctx);
retval = -EINVAL;
}
--
2.47.3