[PATCH v13 34/36] sched/deadline: Warn when a runqueue keeps the bandwidth of a task
From: Mathieu Desnoyers
Date: Tue Dec 01 2026 - 12:46:55 EST
The this_bw of a runqueue counts the bandwidth of each task whose
dl_bw_oncpu is set. add_rq_bw() sets the flag and sub_rq_bw() clears
it. Two states would leave a bandwidth in this_bw for ever:
- A task which left the deadline class still has its flag set, and
moves to another CPU. Only the deadline class takes the bandwidth
off the old runqueue: dequeue_task_dl() for a queued task, and
migrate_task_rq_dl() for a waking one. So nothing takes it off.
- task_non_contending() or inactive_task_timer() releases the
bandwidth of a task while its flag is still set. They then call
__dl_clear_params(), which clears both the flag and dl_bw. After
that, nothing can take the bandwidth off the runqueue.
Without a check, a test sees such a leak only later, as a this_bw
which does not return to its starting value. It cannot tell which
path leaked.
Warn about the first state in set_task_cpu(). Warn about the second
in task_non_contending() and inactive_task_timer(), before they call
__dl_clear_params(). No warning fires in the selftests of this series.
A review of the accounting code by an LLM suggested these checks.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
---
kernel/sched/core.c | 3 +++
kernel/sched/deadline.c | 4 ++++
2 files changed, 7 insertions(+)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 2700c8e98b0e..879ac7d6cdfd 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3517,6 +3517,9 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
*/
WARN_ON_ONCE(!cpu_online(new_cpu));
+ /* A task of another class would leave its bandwidth in this_bw. */
+ WARN_ON_ONCE(!dl_task(p) && p->dl.dl_bw_oncpu);
+
/*
* Proxy execution can move a blocked task's scheduling context to any
* CPU without moving its migration-disabled execution context. The
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index e759e4f27cc7..8892523ada9d 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -904,6 +904,8 @@ static void task_non_contending(struct sched_dl_entity *dl_se, bool dl_task)
if (!dl_task || READ_ONCE(p->__state) == TASK_DEAD) {
if (dl_task && READ_ONCE(p->__state) == TASK_DEAD)
sub_rq_bw(dl_se, &rq->dl);
+ /* No runqueue may still count dl_bw. */
+ WARN_ON_ONCE(dl_se->dl_bw_oncpu);
dl_task_release_bw(p);
cancel_replenish_timer(dl_se);
__dl_clear_params(dl_se);
@@ -2848,6 +2850,8 @@ static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
dl_se->dl_non_contending = 0;
}
+ /* No runqueue may still count dl_bw. */
+ WARN_ON_ONCE(dl_se->dl_bw_oncpu);
dl_task_release_bw(p);
cancel_replenish_timer(dl_se);
__dl_clear_params(dl_se);
--
2.47.3