[PATCH v14 34/39] sched/deadline: Count a task that departs while a rebuild walks the tasks
From: Mathieu Desnoyers
Date: Thu Dec 03 2026 - 10:02:22 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. The new dl_task_count_cpu() picks the CPU as the
walk does in dl_task_rebuild_cpu(). But it does not ask the cpuset for
the CPUs of the task. That needs cpuset_mutex, and dl_task_depart()
runs under the rq lock. If no CPU of the mask of the task is active,
leave the task out. A rebuild drops a bandwidth in the same way when
the CPU recorded with it is no longer active.
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. "selftests/sched: Test PI against
hotplug and rebuilds in deadline_bw_test", later in this series, adds
the case. The runs used v13 of this series, which had an earlier
version of this fix. The case failed in 35 of 43 runs without the fix,
and passed in 35 of 35 with it.
Fixes: f9a25f776d78 ("cpusets: Rebuild root domain deadline accounting information")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
kernel/sched/deadline.c | 77 ++++++++++++++++++++++++++++++++++-------
1 file changed, 65 insertions(+), 12 deletions(-)
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 871507b37759..02c6e2c20027 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -223,6 +223,8 @@ __dl_overflow(struct dl_bw *dl_b, unsigned long cap, u64 old_bw, u64 new_bw)
cap_scale(dl_b->bw, cap) < dl_b->total_bw - old_bw + new_bw;
}
+static unsigned int dl_task_count_cpu(struct task_struct *p);
+
/*
* A task that leaves SCHED_DEADLINE, or dies, before its 0-lag time keeps
* its bandwidth in total_bw until that time. Then the inactive timer
@@ -253,12 +255,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 +380,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 +421,20 @@ 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, on the CPU the walk would have picked. If
+ * no CPU of its mask 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()) {
+ cpu = dl_task_count_cpu(p);
+ if (cpu < nr_cpu_ids)
+ dl_task_count_bw(p, cpu);
+ }
list_add(&dl_se->dl_departed_node, &dl_departed_list);
}
@@ -4404,13 +4439,16 @@ static void dl_get_task_effective_cpus(struct task_struct *p, struct cpumask *cp
* throttled task and its bandwidth on any active CPU. The task runs there
* until it blocks. Inside a rebuild, that move leaves the record on the
* CPU that went offline.
+ *
+ * With @ask_cpuset, ask the cpuset for the CPUs of @p if no CPU of its
+ * mask is active. That needs cpuset_mutex.
*/
-static unsigned int dl_task_rebuild_cpu(struct task_struct *p)
+static unsigned int __dl_task_rebuild_cpu(struct task_struct *p, bool ask_cpuset)
{
struct cpumask *msk = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
/*
* Read the record once. The last schedule of a dead task clears it
- * under its rq lock, and the caller holds only the pi_lock.
+ * under its rq lock, which some callers do not hold.
*/
int recorded = READ_ONCE(p->dl.dl_bw_cpu);
unsigned int cpu;
@@ -4427,7 +4465,7 @@ static unsigned int dl_task_rebuild_cpu(struct task_struct *p)
if (cpu_active(cpu) && task_on_rq_queued(p) &&
(cpu == recorded || !cpumask_test_cpu(cpu, &p->cpus_mask)))
return cpu;
- if (cpumask_empty(msk)) {
+ if (cpumask_empty(msk) && ask_cpuset) {
dl_get_task_effective_cpus(p, msk);
cpumask_and(msk, msk, cpu_active_mask);
}
@@ -4460,6 +4498,22 @@ static unsigned int dl_task_rebuild_cpu(struct task_struct *p)
return cpumask_first(msk);
}
+static unsigned int dl_task_rebuild_cpu(struct task_struct *p)
+{
+ return __dl_task_rebuild_cpu(p, true);
+}
+
+/*
+ * Return the CPU on which the walk of the task list would count @p, for a
+ * task counted outside the walk. Unlike the walk, do not ask the cpuset.
+ * Return nr_cpu_ids if no CPU of the mask of @p is active. The caller must
+ * disable interrupts.
+ */
+static unsigned int dl_task_count_cpu(struct task_struct *p)
+{
+ return __dl_task_rebuild_cpu(p, false);
+}
+
/* From dl_rebuild_rd_accounting(), under the locks it asserts. */
void dl_add_task_root_domain(struct task_struct *p)
{
@@ -4502,9 +4556,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