Re: [PATCH v7 28/40] sched/deadline: Keep a task in the root domain that counts its bandwidth
From: sashiko-bot
Date: Tue Nov 17 2026 - 10:33:04 EST
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] sched/deadline: missing wake_cpu update in
__set_cpus_allowed_ptr_locked()
- [Medium] sched/deadline: wrong root domain in
dl_task_offline_migration() on rebuild
- [Medium] sched/deadline: missing READ_ONCE() on dl_bw_cpu in
dl_task_rebuild_cpu()
--
commit fecf231094845bbb7da45c72106b5c90fffb123d
Author: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
sched/deadline: Keep a task in the root domain that counts its bandwidth
This commit adds dl_task_bw_span() and updates CPU selection paths for
SCHED_DEADLINE tasks to prefer CPUs in the root domain that accounts for
the task's bandwidth.
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index e9f80407f75be..dc270a23179d5 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -3264,6 +3264,23 @@ static int __set_cpus_allowed_ptr_locked(struct task_struct *p,
> dest_cpu = ctx->dl_bw_cpu;
> if (!task_on_rq_queued(p))
> p->wake_cpu = dest_cpu;
> + } else {
> + const struct cpumask *span = dl_task_bw_span(p);
> + unsigned int cpu = nr_cpu_ids;
> +
> + /*
> + * No room was made for @p in another root domain. Keep @p
> + * in the root domain that counts its bandwidth, if the new
> + * mask holds one of its CPUs.
> + */
> + if (span && !cpumask_test_cpu(dest_cpu, span))
> + cpu = cpumask_first_and_and(cpu_valid_mask,
> + ctx->new_mask, span);
> + if (cpu < nr_cpu_ids) {
> + dest_cpu = cpu;
> + if (!task_on_rq_queued(p))
> + p->wake_cpu = dest_cpu;
> + }
> }
[Severity: Medium]
Does this leave p->wake_cpu stale when cpumask_any_and_distribute()
already picked a dest_cpu inside span?
When cpumask_test_cpu(dest_cpu, span) is true, cpu stays at nr_cpu_ids and
p->wake_cpu is not updated for a sleeping task. Because affine_move_task()
does not move sleeping tasks, p->wake_cpu remains on the old CPU (such as
an offlining housekeeping CPU during cpuset_hotplug_update_tasks()). If p
then wakes up during the subsequent sched domain rebuild while
dl_task_bw_span(p) returns NULL, select_task_rq() and select_fallback_rq()
can place p on an isolated CPU in def_root_domain and move its bandwidth
without admission control.
Updating p->wake_cpu whenever span is non-NULL and dest_cpu is in span
(and !task_on_rq_queued(p)) would avoid this.
[ ... ]
> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index 6c6a4d9cd0548..e5f77d3c5fa03 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
[ ... ]
> @@ -901,18 +903,36 @@ static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
>
> static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
> {
> + const struct cpumask *span;
> struct rq *later_rq = NULL;
> struct dl_bw *dl_b;
>
> later_rq = find_lock_later_rq(p, rq);
> + /*
> + * find_lock_later_rq() looks in the root domain of @rq. Once @rq is
> + * offline, a rebuild attaches it to def_root_domain. That root domain
> + * may not count @p. Do not take @p out of the root domain that counts
> + * it while @p has an active CPU there.
> + */
> + span = dl_task_bw_span(p);
> + if (later_rq && span && !cpumask_test_cpu(later_rq->cpu, span) &&
> + cpumask_first_and_and(cpu_active_mask, p->cpus_ptr, span) < nr_cpu_ids) {
> + double_unlock_balance(rq, later_rq);
> + later_rq = NULL;
> + }
> if (!later_rq) {
> int cpu;
>
> /*
> * If we cannot preempt any rq, fall back to pick any
> - * online CPU:
> + * online CPU. Try the root domain that counts @p first.
> */
> - cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr);
> + cpu = nr_cpu_ids;
> + if (span)
> + cpu = cpumask_first_and_and(cpu_active_mask,
> + p->cpus_ptr, span);
> + if (cpu >= nr_cpu_ids)
> + cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr);
[Severity: Medium]
Can dl_task_offline_migration() still move a throttled task and its
bandwidth into def_root_domain when dl_task_timer() fires during a sched
domain rebuild?
CPU0 CPU1 (<hardirq>)
sched_cpu_deactivate(H)
sched_set_rq_offline(rq_H)
cpuset_cpu_inactive(H)
partition_sched_domains_locked()
dl_bw_rebuild_begin()
detach_destroy_domains()
cpu_attach_domain(def_rd, H)
dl_task_timer(p)
dl_task_offline_migration(rq_H, p)
find_lock_later_rq() -> iso_rq
dl_task_bw_span(p) -> NULL
p->dl.dl_bw_cpu = iso_rq->cpu
dl_rebuild_rd_accounting()
dl_add_task_root_domain(p)
dl_task_rebuild_cpu(p) -> iso_rq->cpu
While dl_bw_rebuild_seq is odd, rq_H is already attached to
def_root_domain, so find_lock_later_rq() searches def_root_domain (or
cpumask_any_and() picks the first active CPU in p->cpus_ptr, which can be
an isolated CPU). Because dl_task_bw_span(p) returns NULL during the
rebuild, the check above does not reject the isolated later_rq.
Unlike migrate_task_rq_dl() and task_woken_dl(), which skip moving
bandwidth while dl_bw_rebuilding() is true, dl_task_offline_migration()
then unconditionally subtracts p->dl.dl_bw from src_rq->rd->dl_bw (which
can underflow total_bw before dl_clear_root_domains() runs), adds it to
def_root_domain, and overwrites p->dl.dl_bw_cpu with the isolated CPU.
Both the rebuild and later wakeups then treat def_root_domain as the root
domain that counts p.
[ ... ]
> @@ -3987,17 +4075,37 @@ static unsigned int dl_task_rebuild_cpu(struct task_struct *p)
> */
> cpumask_and(msk, &p->cpus_mask, cpu_active_mask);
> cpu = task_cpu(p);
> - if (cpumask_test_cpu(cpu, msk) ||
> - (cpu == recorded && cpu_active(cpu) && task_on_rq_queued(p)))
> + if (cpu == recorded && cpu_active(cpu) && task_on_rq_queued(p))
> return cpu;
> if (cpumask_empty(msk)) {
> if (recorded >= 0 && cpu_active(recorded))
> return recorded;
> dl_get_task_effective_cpus(p, msk);
> cpumask_and(msk, msk, cpu_active_mask);
> - } else if (recorded >= 0 && cpumask_test_cpu(recorded, msk)) {
> - return recorded;
> }
> + /*
> + * Keep the bandwidth where it was counted: with the housekeeping
> + * CPUs, or with the CPUs that isolcpus="domain" isolates. The root
> + * domains are new, and the CPU recorded can be offline. So prefer a
> + * CPU of the mask of the same kind as the CPU recorded: housekeeping
> + * or not.
> + */
> + if (recorded >= 0) {
> + const struct cpumask *hk = housekeeping_cpumask(HK_TYPE_DOMAIN_BOOT);
> + bool rec_hk = cpumask_test_cpu(recorded, hk);
> + unsigned int same;
> +
> + if (cpumask_test_cpu(cpu, msk) && cpumask_test_cpu(cpu, hk) == rec_hk)
> + return cpu;
> + if (cpumask_test_cpu(recorded, msk))
> + return recorded;
> + for_each_cpu(same, msk) {
> + if (cpumask_test_cpu(same, hk) == rec_hk)
> + return same;
> + }
> + }
[Severity: Medium]
Should p->dl.dl_bw_cpu be read with READ_ONCE() when initializing recorded
in dl_task_rebuild_cpu()?
dl_add_task_root_domain() calls dl_task_rebuild_cpu(p) while holding only
p->pi_lock, before acquiring dl_departed_lock. Concurrently, a dying
deadline task in its final __schedule() holds rq->lock and
dl_departed_lock (not p->pi_lock) and can write p->dl.dl_bw_cpu = -1 via
task_non_contending() -> dl_task_release_bw(). Because recorded is loaded
with a plain read and used after calling housekeeping_cpumask(), the
compiler is free to reload p->dl.dl_bw_cpu after the recorded >= 0 check
and pass -1 to cpumask_test_cpu().
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1794922146.git.mathieu.desnoyers@xxxxxxxxxxxx?part=28