[PATCH v13 31/36] sched/deadline: Count a task that departs while a rebuild walks the tasks
From: Mathieu Desnoyers
Date: Tue Dec 01 2026 - 12:46:24 EST
dl_rebuild_rd_accounting() rebuilds the root domains' accounting in
two steps. First, dl_clear_root_domains() clears the root domains.
Then it puts back the bandwidth of the departed tasks, which left
SCHED_DEADLINE or died before their 0-lag time. Second, the walk of the
task list counts each deadline task.
A task can leave the class after the clear, before the walk reaches
it: priority inheritance can deboost it. The walk then finds a task
out of the class, and skips it. Neither step counts its bandwidth. The
comment above dl_departed_list lists this as a known gap.
In this example, the deadline task W waits with a timeout on an
rt_mutex that T holds. T was a deadline task too. While W boosted it,
sched_setattr() switched T to SCHED_OTHER. The boost keeps T in the
class, with its bandwidth. W runs on CPU 1. CPU 0 rebuilds the
accounting, for example after a write to cpuset.cpus.partition.
CPU 0 CPU 1
----- -----
dl_rebuild_rd_accounting()
dl_clear_root_domains()
clear total_bw
put back the departed tasks
W times out: remove_waiter()
rt_mutex_setprio(T)
switched_from_dl(T)
dl_task_depart(T)
T joins the departed list
walk of the task list reaches T
T is out of the class: skip it
total_bw then misses the bandwidth of T until its 0-lag time. Meanwhile,
sched_setattr() can admit tasks beyond the capacity of the root domain.
Fix this by counting such a task in dl_task_depart(), as the walk
would have. dl_bw_move_blocked() already tells whether the walk has yet
to reach the task. Count the task on the CPU it departs from, or else
on the CPU recorded with it, or else on an active CPU of its mask. If
none is active, leave it out, as the walk does.
A task that dies in the same window departs too. The walk counted it
from the departed list, unless release_task() had taken it off the
task list. dl_task_depart() now counts it in both cases.
This was found by reading the code, and by a Spin model of the
accounting. The pi_deboost_during_rebuild case of deadline_bw_test
races the timeout with the rebuild. It failed in 35 of 43 runs without
this fix, and passed in 35 of 35 with it. "selftests/sched: Test PI
against hotplug and rebuilds in deadline_bw_test", later in this
series, adds the case.
Fixes: f9a25f776d78 ("cpusets: Rebuild root domain deadline accounting information")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
kernel/sched/deadline.c | 54 ++++++++++++++++++++++++++++++++++-------
1 file changed, 45 insertions(+), 9 deletions(-)
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 7aa6c3f7e99d..ef97b07c0aa2 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -253,12 +253,14 @@ __dl_overflow(struct dl_bw *dl_b, unsigned long cap, u64 old_bw, u64 new_bw)
* harmless.
*
* A rebuild drops the bandwidth when the CPU recorded is no longer
- * active. It also drops the bandwidth of a task that dies while the
- * rebuild runs. The walk of the task list does not find that task, and
- * it was not dead yet when the list was put back. The same happens to a
- * task that priority inheritance deboosts while the rebuild runs. It was
- * in the class when the list was put back, and out of it when the walk
- * reaches it.
+ * active. It also drops the bandwidth of a task that release_task()
+ * took off the task list, if its last schedule comes after the walk.
+ * The walk does not find that task, and it was not on the departed
+ * list when the list was put back.
+ *
+ * A task can also depart after the list was put back, and before the
+ * walk reaches it. Priority inheritance can deboost it, or it can die.
+ * dl_task_depart() then counts it, as the walk would have.
*
* dl_departed_lock protects the list and both counts. Take it with
* interrupts disabled, under the task's rq lock or pi_lock or in the
@@ -376,6 +378,23 @@ bool dl_task_holds_bw(struct task_struct *p)
return dl_se_holds_bw(&p->dl);
}
+/*
+ * Add the bandwidth of @p to the root domain of @cpu, for a task that no
+ * root domain counts now. Record @cpu, and date the record with the
+ * current dl_bw_gen. The caller holds dl_departed_lock.
+ */
+static void dl_task_count_bw(struct task_struct *p, int cpu)
+{
+ struct dl_bw *dl_b = dl_bw_of(cpu);
+
+ lockdep_assert_held(&dl_departed_lock);
+ raw_spin_lock(&dl_b->lock);
+ __dl_add(dl_b, p->dl.dl_bw, dl_bw_cpus(cpu));
+ raw_spin_unlock(&dl_b->lock);
+ dl_record_bw_cpu(&p->dl, cpu);
+ p->dl.dl_bw_gen = dl_bw_gen;
+}
+
/*
* Put @p on dl_departed_list. The root domain of the CPU recorded with it
* keeps its bandwidth. @p leaves SCHED_DEADLINE, or dies, before its
@@ -400,6 +419,24 @@ static void dl_task_depart(struct task_struct *p)
if (dl_se->dl_bw_gen == dl_bw_gen && !dl_bw_rebuilding() && cpu_active(cpu) &&
cpu_rq(cpu)->rd == cpu_rq(dl_se->dl_bw_cpu)->rd)
dl_record_bw_cpu(dl_se, cpu);
+ /*
+ * dl_clear_root_domains() counted the departed tasks, and the walk
+ * of the task list counts the deadline tasks. A task that departs
+ * after the clear, before the walk reaches it, would be counted by
+ * neither. Count it here, as the walk would have. Use the CPU it
+ * departs from, or else the CPU recorded, or else an active CPU of
+ * its mask. If none is active, leave it out. While the sched domains
+ * are rebuilt, the clear is still ahead, and finds the task on the
+ * list.
+ */
+ if (dl_bw_move_blocked(p) && !dl_bw_rebuilding()) {
+ if (!cpu_active(cpu))
+ cpu = dl_se->dl_bw_cpu;
+ if (!cpu_active(cpu))
+ cpu = cpumask_first_and(cpu_active_mask, &p->cpus_mask);
+ if (cpu < nr_cpu_ids)
+ dl_task_count_bw(p, cpu);
+ }
list_add(&dl_se->dl_departed_node, &dl_departed_list);
}
@@ -4539,9 +4576,8 @@ void dl_add_task_root_domain(struct task_struct *p)
* dl_clear_root_domains() put it back as a departed task, and
* priority inheritance boosted it into the class since. Or it died
* since the rebuild started, and dl_task_release_bw() released its
- * bandwidth. A task that died since with its 0-lag time ahead is on
- * the departed list. Count it in the root domain of the CPU
- * recorded, if that CPU is active.
+ * bandwidth. Or it departed before the walk reached it, and
+ * dl_task_depart() counted it.
*/
if (p->dl.dl_bw_gen == dl_bw_gen)
goto unlock;
--
2.47.3