Re: [PATCH v8 14/33] sched/deadline: Move a task's bandwidth to the root domain it runs in

From: sashiko-bot

Date: Thu Nov 19 2026 - 15:53:38 EST


Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] sched/deadline: dl_bw_move_blocked() drops bw of tasks
skipped by walk
- [Medium] sched/deadline: task_woken_dl() moves bw to old rd on pinned
wakeup
- [Medium] sched/deadline: dl_task_move_bw() moves departing bandwidth
of PI-boosted task
--

commit 692ba1dcff2566600d665231dbe2bb9a5d88adc8
Author: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>

sched/deadline: Move a task's bandwidth to the root domain it runs in

This commit moves a deadline task's bandwidth in migrate_task_rq_dl() and
task_woken_dl() when the task migrates to or wakes up on an active CPU in a
root domain different from the one currently accounting its bandwidth, while
blocking moves during sched domain rebuilds and tasklist accounting walks.

> diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
> index df55aba27ddc3..fb8f32a31cf91 100644
> --- a/kernel/sched/deadline.c
> +++ b/kernel/sched/deadline.c
> @@ -268,6 +268,71 @@ static LIST_HEAD(dl_departed_list);
> static DEFINE_RAW_SPINLOCK(dl_departed_lock);
> static unsigned int dl_bw_gen;
>
> +/*
> + * Odd while the sched domains are rebuilt: from before the first CPU
> + * leaves its root domain until dl_clear_root_domains(). Meanwhile the
> + * CPUs change root domains one by one. Housekeeping CPUs pass through
> + * def_root_domain, next to the CPUs that isolcpus="domain" isolates. So
> + * the root domain of the CPU recorded with a task says nothing about
> + * where its bandwidth is counted. The new root domains count no task
> + * yet.
> + *
> + * Changed under dl_departed_lock. dl_task_bw_elsewhere() reads it
> + * without the lock.
> + */
> +static unsigned int dl_bw_rebuild_seq;
> +
> +/*
> + * True while dl_rebuild_rd_accounting() walks the task list: from
> + * dl_clear_root_domains() until the walk ends. The root domains are final
> + * by then. A task the walk has not reached yet has a dl_bw_gen that is
> + * not current. Changed under dl_departed_lock. dl_task_bw_elsewhere()
> + * reads it without the lock.
> + */
> +static bool dl_bw_walking;
> +
> +/* Return true while the sched domains are rebuilt. */
> +static inline bool dl_bw_rebuilding(void)
> +{
> + return READ_ONCE(dl_bw_rebuild_seq) & 1;
> +}
> +
> +/*
> + * Return true if the bandwidth of @p must not move now. Either the sched
> + * domains are rebuilt, or the walk of the task list that follows has not
> + * reached @p yet. The walk then counts @p. A move must not count it
> + * first.
> + *
> + * The answer is exact under dl_departed_lock. Without it, the answer is a
> + * hint: dl_task_move_bw() tests again under the lock.
> + */
> +static inline bool dl_bw_move_blocked(struct task_struct *p)
> +{
> + if (dl_bw_rebuilding())
> + return true;
> + return READ_ONCE(dl_bw_walking) &&
> + READ_ONCE(p->dl.dl_bw_gen) != READ_ONCE(dl_bw_gen);
> +}

[Severity: Medium]
Can dl_bw_move_blocked() prevent a task that dl_add_task_root_domain()
already visited and skipped from adding its bandwidth when it falls back
to an active CPU before the tasklist walk finishes?

When dl_add_task_root_domain() visits a task that currently has no active
CPU in its mask, it returns early without updating p->dl.dl_bw_gen,
relying on a later move of the task to an active CPU to count its
bandwidth. However, p->dl.dl_bw_gen != dl_bw_gen leaves the skipped task
indistinguishable from a task that the walk has not reached yet:

CPU0 CPU1
dl_rebuild_rd_accounting()
dl_clear_root_domains()
dl_bw_gen++
dl_bw_walking = true
for_each_process_thread(g, p)
dl_add_task_root_domain(p)
dl_task_rebuild_cpu(p) >= nr_cpu_ids
return (p->dl.dl_bw_gen != dl_bw_gen)
try_to_wake_up(p)
select_fallback_rq() -> new_cpu
migrate_task_rq_dl(p, new_cpu)
dl_task_bw_elsewhere(p, new_cpu)
dl_bw_move_blocked(p) -> true
ttwu_do_activate()
task_woken_dl(rq, p)
dl_bw_move_blocked(p) -> true
... walks remaining tasks ...
dl_bw_walk_end()
dl_bw_walking = false <-- p runs on new_cpu with bandwidth
uncounted in new_cpu's rd

Because the walk has already passed p and both migrate_task_rq_dl() and
task_woken_dl() were blocked by dl_bw_move_blocked(), p runs in new_cpu's
root domain without its bandwidth counted there, allowing that root domain
to admit deadline tasks beyond its capacity.

[ ... ]

> @@ -2920,11 +2985,82 @@ select_task_rq_dl(struct task_struct *p, int cpu, int flags)
> return cpu;
> }
>
> -static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
> +/*
> + * Return true if the root domain of @cpu may not count the bandwidth of
> + * @p. Either another root domain counts it, or a rebuild left @p out.
> + * Test without dl_departed_lock, so that a move inside a root domain
> + * takes no lock. dl_task_move_bw() tests again under it.
> + *
> + * Return false if @cpu is not active. A migration-disabled task can wake
> + * up on a CPU that goes offline, after it joined the default root domain.
> + * Keep the bandwidth where it is counted. balance_push() moves the task
> + * off that CPU once it enables migration, or its next wakeup does once
> + * the CPU is offline. That migration moves the bandwidth if needed.
> + *
> + * Return false while the sched domains are rebuilt. The CPUs then move
> + * to their new root domains one by one. That alone can make the two root
> + * domains differ. The rebuild of the accounting that follows counts @p.
> + * Return false also while that rebuild has not reached @p. Once it has
> + * counted @p, the root domains are final, and a move is right again.
> + */
> +static bool dl_task_bw_elsewhere(struct task_struct *p, int cpu)
> +{
> + int bw_cpu = READ_ONCE(p->dl.dl_bw_cpu);
> +
> + if (bw_cpu < 0 || !p->dl.dl_bw || !cpu_active(cpu) ||
> + dl_bw_move_blocked(p))
> + return false;
> + return READ_ONCE(p->dl.dl_bw_gen) != READ_ONCE(dl_bw_gen) ||
> + cpu_rq(bw_cpu)->rd != cpu_rq(cpu)->rd;
> +}
> +
> +/*
> + * Move the bandwidth of @p to the root domain of @new_cpu. Count it there
> + * also if a rebuild left @p out. Add it even without room, since @p runs
> + * there. The caller holds the rq lock of @p, or its pi_lock for a wakeup.
> + */
> +static void dl_task_move_bw(struct task_struct *p, int new_cpu)
> +{
> + struct dl_bw *dl_b;
> + int cpu;
> +
> + if (dl_entity_is_special(&p->dl) || !p->dl.dl_bw)
> + return;
> +
> + guard(raw_spinlock)(&dl_departed_lock);
> + cpu = p->dl.dl_bw_cpu;
> + if (cpu < 0 || dl_bw_move_blocked(p))
> + return;
> + if (p->dl.dl_bw_gen == dl_bw_gen) {
> + if (cpu_rq(cpu)->rd == cpu_rq(new_cpu)->rd)
> + return;
> + dl_b = dl_bw_of(cpu);
> + raw_spin_lock(&dl_b->lock);
> + __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(cpu));
> + raw_spin_unlock(&dl_b->lock);
> + }
> + dl_b = dl_bw_of(new_cpu);
> + raw_spin_lock(&dl_b->lock);
> + __dl_add(dl_b, p->dl.dl_bw, dl_bw_cpus(new_cpu));
> + raw_spin_unlock(&dl_b->lock);
> + p->dl.dl_bw_cpu = new_cpu;
> + p->dl.dl_bw_gen = dl_bw_gen;
> +}

[Severity: Medium]
Can dl_task_bw_elsewhere() and dl_task_move_bw() move the departing
bandwidth of a non-deadline task that is PI-boosted back into
dl_sched_class before its 0-lag time?

When a task leaves SCHED_DEADLINE before its 0-lag time, p->dl.dl_bw
remains non-zero and p->dl.dl_bw_cpu records the original root domain
where its departing bandwidth must stay accounted until
inactive_task_timer() fires, as noted in switched_to_dl():

kernel/sched/deadline.c:switched_to_dl() {
...
* again. So take it off the list. The task keeps the CPU recorded. It
* takes the bandwidth from the root domain of that CPU, even if it
* moved to another root domain meanwhile.
...
}

Because neither dl_task_bw_elsewhere() nor dl_task_move_bw() checks
task_has_dl_policy(p), if that departed task moves to another root domain
under SCHED_OTHER and is PI-boosted into dl_sched_class before its 0-lag
time, waking or migrating it in the new root domain subtracts its
departing bandwidth from the original root domain early and adds it to the
new root domain without admission control. Checking task_has_dl_policy(p)
in dl_task_bw_elsewhere() and dl_task_move_bw() would keep departing
bandwidth pinned to the original root domain.

[ ... ]

> @@ -3587,6 +3723,15 @@ static void pull_dl_task(struct rq *this_rq)
> */
> static void task_woken_dl(struct rq *rq, struct task_struct *p)
> {
> + /*
> + * A task can wake up on the CPU it slept on, with no migration,
> + * while another root domain counts its bandwidth. The bandwidth
> + * moved while the task slept out of its mask, and the mask then got
> + * that CPU back. Move the bandwidth to this root domain.
> + */
> + if (unlikely(dl_task_bw_elsewhere(p, cpu_of(rq))))
> + dl_task_move_bw(p, cpu_of(rq));

[Severity: Medium]
If a task sleeps inside a migrate_disable() section on an active CPU while
its affinity and bandwidth move to another root domain, does
task_woken_dl() move the bandwidth back to the old root domain when the
task wakes up on its pinned CPU?

When p blocks under migrate_disable() on active CPU B (in root domain B)
and cpuset_attach_task() -> set_cpus_allowed_dl() moves p->cpus_mask and
p->dl.dl_bw_cpu to root domain A while affine_move_task() waits for
migrate_enable(), waking p to finish its migrate_disable() section selects
CPU B via p->cpus_ptr without migration. Because CPU B is still active
and dl_task_bw_elsewhere() does not check is_migration_disabled(p) or
whether the CPU is in p->cpus_mask, task_woken_dl() moves p's bandwidth
from root domain A back to root domain B. That frees the reserved
bandwidth in root domain A before migrate_enable() migrates p to root
domain A and re-adds its bandwidth without admission control.

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