[PATCH v14 36/39] sched/deadline: Keep a reaped deadline task in a rebuild
From: Mathieu Desnoyers
Date: Thu Dec 03 2026 - 10:03:31 EST
A rebuild of the root domains' accounting counts tasks from two places:
dl_departed_list, and the task list. The walk of the task list counts a
dying deadline task until its last schedule. That schedule releases the
bandwidth, or puts the task on dl_departed_list until its 0-lag time.
But release_task() can take a task off the task list before its last
schedule. A thread other than the group leader reaps itself in
exit_notify(). A parent can reap a zombie child while the child still
runs. A rebuild in that window finds the task in neither place, and
drops its bandwidth. total_bw then misses it until the last schedule,
or until the 0-lag time if that is later. Meanwhile, sched_setattr()
would admit tasks beyond the capacity of the root domain.
Fix this by putting such a task on dl_departed_list from release_task(),
in the new dl_task_unhash(). It runs under tasklist_lock, before the
task leaves the task list, so no walk runs in between. A rebuild then
counts the task with the departed tasks. Its last schedule releases the
bandwidth and takes it off the list, or leaves both to the inactive
timer.
dl_task_unhash() reads dl_bw under the rq lock of the task.
__sched_setscheduler() admits under that lock, and can still admit the
task at this point. It tests exit_state, which the task sets without
that lock. So release_task() now takes the rq lock of each task it
reaps.
A reap after dl_clear_root_domains() and before the walk is covered by
"sched/deadline: Count a task that departs while a rebuild walks the
tasks". dl_task_depart() counts the task then, as the walk would have.
A boost back into the class takes a task off the list, in
dl_task_undepart(). Keep a task that has exited on it: it may be off the
task list already.
A task on the list can then still run in the class. This needs four
more changes:
- dl_clear_root_domains() drops the bandwidth of a task on the list
whose recorded CPU is inactive. A task that still runs in the class
can run on another CPU than the one recorded. Count it on the CPU
that the walk would pick.
- If no CPU of its mask is active, the clear leaves the task out. If
the walk finds the task, dl_add_task_root_domain() counts it: the
walk can ask the cpuset.
- If the 0-lag time is ahead, the last schedule of the task calls
dl_task_depart() again. Update the CPU recorded there, even for a
task already on the list.
- A boost can bring an exited task back into the class after an
earlier rebuild dropped its bandwidth. During a rebuild of the sched
domains, dl_task_undepart() cannot count it yet, and the walk skips
a task on the list. So dl_task_undepart() asks the clear that ends
the rebuild to count the task, through dl_bw_skip_gen.
A Spin model of the accounting found this case. It was not reproduced:
the window from release_task() to the last schedule is short. With this
fix and the one named above, the model finds no error for two tasks and
one root domain.
Fixes: f9a25f776d78 ("cpusets: Rebuild root domain deadline accounting information")
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
include/linux/sched.h | 4 +-
include/linux/sched/deadline.h | 1 +
kernel/exit.c | 2 +
kernel/sched/deadline.c | 132 ++++++++++++++++++++++++---------
4 files changed, 104 insertions(+), 35 deletions(-)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e27d341ca763..d2531a4e3dba 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -765,7 +765,9 @@ struct sched_dl_entity {
* @dl_bw_skip_gen is the value of dl_bw_gen when a rebuild of the
* accounting left the task out. Before the walk of the task list
* ends, a move of the task can then count the bandwidth. So can a
- * boost of a departed task back into the class.
+ * boost of a departed task back into the class. dl_task_undepart()
+ * can also set it one ahead, to ask the next dl_clear_root_domains()
+ * to count the task.
*
* @dl_bw_resv_cpu is a CPU of the root domain where a cpuset attach
* in progress reserved room for the bandwidth, or -1.
diff --git a/include/linux/sched/deadline.h b/include/linux/sched/deadline.h
index deddfca67ec2..1297a7a8d3fd 100644
--- a/include/linux/sched/deadline.h
+++ b/include/linux/sched/deadline.h
@@ -36,6 +36,7 @@ extern void dl_clear_root_domains(void);
extern void dl_bw_rebuild_begin(void);
extern void dl_rebuild_rd_accounting(void);
extern bool dl_task_holds_bw(struct task_struct *p);
+extern void dl_task_unhash(struct task_struct *p);
/*
* Keep a rebuild of the root domains' accounting and an admission of
* bandwidth from running at the same time. With cpusets, cpuset_mutex
diff --git a/kernel/exit.c b/kernel/exit.c
index d5e7bc2af804..1656b4d1b661 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -13,6 +13,7 @@
#include <linux/sched/task.h>
#include <linux/sched/task_stack.h>
#include <linux/sched/cputime.h>
+#include <linux/sched/deadline.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/capability.h>
@@ -264,6 +265,7 @@ void release_task(struct task_struct *p)
write_lock_irq(&tasklist_lock);
ptrace_release_task(p);
+ dl_task_unhash(p);
__exit_signal(&post, p);
/*
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index f2429672358c..1eba22b9010b 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -255,10 +255,11 @@ static unsigned int dl_task_count_cpu(struct task_struct *p);
* harmless.
*
* A rebuild drops the bandwidth when the CPU recorded is no longer
- * 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.
+ * active.
+ *
+ * release_task() can take a task off the task list before its last
+ * schedule. The walk would then miss it. So dl_task_unhash() puts a
+ * reaped deadline task on the list, while it is still in the class.
*
* 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.
@@ -400,8 +401,8 @@ static void dl_task_count_bw(struct task_struct *p, int cpu)
/*
* 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
- * 0-lag time. Do nothing if @p is on the list already. The caller holds
- * the rq lock of @p.
+ * 0-lag time. If @p is on the list already, only update the CPU
+ * recorded. The caller holds the rq lock of @p.
*/
static void dl_task_depart(struct task_struct *p)
{
@@ -410,17 +411,20 @@ static void dl_task_depart(struct task_struct *p)
lockdep_assert_irqs_disabled();
guard(raw_spinlock)(&dl_departed_lock);
- if (!list_empty(&dl_se->dl_departed_node) || dl_se->dl_bw_cpu < 0)
+ if (dl_se->dl_bw_cpu < 0)
return;
/*
- * A rebuild drops the bandwidth if the CPU recorded is not active.
- * That CPU can be one that @p left long ago. Record the CPU @p
- * departs from instead, if it is active and the same root domain
- * counts the bandwidth.
+ * A rebuild counts a departed task on the CPU recorded, if it is
+ * active. That CPU can be one that @p left long ago. Record the CPU
+ * @p departs from instead, if it is active and the same root domain
+ * counts the bandwidth. Do it also for a task on the list already:
+ * dl_task_unhash() put it there, and it may have moved since.
*/
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);
+ if (!list_empty(&dl_se->dl_departed_node))
+ return;
/*
* dl_clear_root_domains() counted the departed tasks, and the walk
* of the task list counts the deadline tasks. A task that departs
@@ -440,6 +444,30 @@ static void dl_task_depart(struct task_struct *p)
list_add(&dl_se->dl_departed_node, &dl_departed_list);
}
+/*
+ * Put @p on dl_departed_list if it is a deadline task with a bandwidth.
+ * release_task() calls this before it takes @p off the task list, so
+ * that a rebuild still counts @p until its last schedule. That schedule,
+ * or the inactive timer after it, takes @p off the list. The caller holds
+ * tasklist_lock for writing: no walk of the task list runs meanwhile.
+ */
+void dl_task_unhash(struct task_struct *p)
+{
+ struct rq_flags rf;
+ struct rq *rq;
+
+ lockdep_assert_held_write(&tasklist_lock);
+ /*
+ * Test dl_bw under the rq lock. __sched_setscheduler() admits under
+ * that lock, and can still admit @p here: @p sets exit_state without
+ * that lock.
+ */
+ rq = task_rq_lock(p, &rf);
+ if (dl_task(p) && dl_task_holds_bw(p))
+ dl_task_depart(p);
+ task_rq_unlock(rq, p, &rf);
+}
+
/*
* Take @p off dl_departed_list. @p comes back to the deadline class, and
* its bandwidth is that of a deadline task again. The caller holds the rq
@@ -459,7 +487,20 @@ static void dl_task_undepart(struct task_struct *p)
if (list_empty(&dl_se->dl_departed_node))
return;
guard(raw_spinlock)(&dl_departed_lock);
- list_del_init(&dl_se->dl_departed_node);
+ /*
+ * Keep a task that has exited on the list. It may be off the task list
+ * already (dl_task_unhash()), and a rebuild must still count it.
+ */
+ if (!p->exit_state)
+ list_del_init(&dl_se->dl_departed_node);
+ else if (dl_se->dl_bw_gen != dl_bw_gen && dl_bw_rebuilding())
+ /*
+ * dl_clear_root_domains() ends this rebuild. It skips
+ * bandwidth that an earlier rebuild dropped, and the walk
+ * after it skips @p too: @p is on the list. Ask the clear to
+ * count @p. It sets dl_bw_gen to dl_bw_gen + 1.
+ */
+ WRITE_ONCE(dl_se->dl_bw_skip_gen, dl_bw_gen + 1);
if (dl_se->dl_bw_gen != dl_bw_gen && !dl_bw_move_blocked(p)) {
unsigned int cpu = dl_task_count_cpu(p);
@@ -4557,9 +4598,9 @@ void dl_add_task_root_domain(struct task_struct *p)
* out of this rebuild. dl_bw_gen then keeps its release from
* removing anything. A later rebuild counts it again. So does
* the move of the task to an active CPU. Mark the task, so
- * that the move does not wait for the end of the walk. Do not
- * leave out a departed task: the code below counts it on the
- * CPU recorded, if that CPU is active.
+ * that the move does not wait for the end of the walk. Leave a
+ * task on the departed list to the code below:
+ * dl_clear_root_domains() already counted it or marked it.
*/
WRITE_ONCE(p->dl.dl_bw_skip_gen, dl_bw_gen);
goto unlock;
@@ -4576,19 +4617,20 @@ void dl_add_task_root_domain(struct task_struct *p)
if (p->dl.dl_bw_gen == dl_bw_gen)
goto unlock;
if (!list_empty(&p->dl.dl_departed_node)) {
- int recorded = p->dl.dl_bw_cpu;
-
/*
* The walk also finds a dead task that departed before an
* earlier rebuild, if it is not reaped yet. If that rebuild
* dropped its bandwidth, dl_clear_root_domains() skipped it,
* and its release removes nothing. Skip it here too.
+ *
+ * The clear counted the other tasks on the list, or dropped a
+ * dead task whose CPU is inactive. It left out a task in the
+ * class with no active CPU in its mask, since it cannot ask
+ * the cpuset. Count that task here.
*/
- if (p->dl.dl_bw_gen != dl_bw_gen - 1)
+ if (p->dl.dl_bw_gen != dl_bw_gen - 1 || cpu >= nr_cpu_ids ||
+ READ_ONCE(p->__state) == TASK_DEAD)
goto unlock;
- if (recorded < 0 || !cpu_active(recorded))
- goto unlock;
- cpu = recorded;
} else if (READ_ONCE(p->__state) == TASK_DEAD && p->dl.dl_bw_cpu < 0) {
/*
* Count nothing for a dead task with no record. Its last
@@ -4743,8 +4785,9 @@ void dl_rebuild_rd_accounting(void)
* the rebuild in dl_bw_gen. Put back the bandwidth of their servers. Put
* back the bandwidth of the tasks on dl_departed_list, which the walk of
* the deadline tasks does not count. Put each in the root domain of the
- * CPU recorded for it, or drop it if that CPU is not active.
- * dl_add_task_root_domain() then adds the deadline tasks.
+ * CPU recorded for it. If that CPU is not active, drop the bandwidth. But
+ * count a task that still runs in the class, on the CPU the walk would
+ * pick. dl_add_task_root_domain() then adds the deadline tasks.
*/
void dl_clear_root_domains(void)
{
@@ -4778,20 +4821,40 @@ void dl_clear_root_domains(void)
/*
* Skip bandwidth that an earlier rebuild dropped. It is not
* counted now, and its release removes nothing
- * (dl_task_release_bw()).
+ * (dl_task_release_bw()). Count it anyway if
+ * dl_task_undepart() asked for it through dl_bw_skip_gen: a
+ * boost brought an exited task back into the class.
*/
- if (dl_se->dl_bw_gen != dl_bw_gen - 1)
+ if (dl_se->dl_bw_gen != dl_bw_gen - 1 &&
+ dl_se->dl_bw_skip_gen != dl_bw_gen)
+ goto left_out;
+ if (cpu < 0)
goto left_out;
/*
- * Drop the bandwidth of a task whose CPU is inactive. A CPU
- * that goes offline is in the default root domain by then.
- * The root domain that counted the task cannot be found
- * from the recorded number. The release then removes
- * nothing. A boost of the task back into the class counts
- * it again (dl_task_undepart()).
+ * Drop the bandwidth of a departed task whose CPU is inactive.
+ * A CPU that goes offline is in the default root domain by
+ * then. The root domain that counted the task cannot be found
+ * from the recorded number. The release then removes nothing.
+ * A boost of the task back into the class counts it again
+ * (dl_task_undepart()).
+ *
+ * A task on the list can also still run in the class. It is a
+ * reaped task before its last schedule, or an exited task that
+ * a boost brought back. The walk leaves it to the clear. Count
+ * it on the CPU the walk would pick. This reads the task
+ * without its locks, so the CPU can be wrong. The bandwidth
+ * then stays in that root domain until the release, or the
+ * next rebuild.
*/
- if (cpu < 0 || !cpu_active(cpu))
- goto left_out;
+ if (!cpu_active(cpu)) {
+ struct task_struct *p = dl_task_of(dl_se);
+
+ if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD)
+ goto left_out;
+ cpu = dl_task_count_cpu(p);
+ if (cpu >= nr_cpu_ids)
+ goto left_out;
+ }
dl_b = dl_bw_of(cpu);
raw_spin_lock(&dl_b->lock);
__dl_add(dl_b, dl_se->dl_bw, dl_bw_cpus(cpu));
@@ -4901,7 +4964,8 @@ static void switched_to_dl(struct rq *rq, struct task_struct *p)
* boosts it back into the class before its 0-lag time. (A policy change
* took it off the list in sched_dl_overflow().) The inactive timer no
* longer removes its bandwidth: the task does when it leaves the class
- * again. So take it off the list. The task keeps the CPU recorded.
+ * again. So take it off the list, unless it has exited
+ * (dl_task_undepart()). The task keeps the CPU recorded.
* When it leaves the class, it takes the bandwidth from the root
* domain of that CPU. If the task moved to another root domain
* meanwhile, its next wakeup or migration there first moves the
--
2.47.3