[PATCH v13 33/36] sched/deadline: Keep a reaped deadline task in a rebuild
From: Mathieu Desnoyers
Date: Tue Dec 01 2026 - 12:46:28 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.
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 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/deadline.h | 1 +
kernel/exit.c | 2 ++
kernel/sched/deadline.c | 37 +++++++++++++++++++++++++++++-----
3 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/include/linux/sched/deadline.h b/include/linux/sched/deadline.h
index bb8c9c1cae5b..1b245fbb31a8 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 d84a459e7a64..e759e4f27cc7 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -253,10 +253,11 @@ __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 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.
@@ -442,6 +443,27 @@ 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);
+ if (!READ_ONCE(p->dl.dl_bw))
+ return;
+ 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. @cpu is the CPU of @p.
@@ -462,7 +484,12 @@ static void dl_task_undepart(struct task_struct *p, int cpu)
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);
if (dl_se->dl_bw_gen != dl_bw_gen && !dl_bw_move_blocked(p) &&
cpu_active(cpu))
dl_task_count_bw(p, cpu);
--
2.47.3