mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: juri.lelli@redhat.com, peterz@infradead.org, mingo@redhat.com,
	vincent.guittot@linaro.org
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	tglx@kernel.org, will@kernel.org, boqun@kernel.org,
	dietmar.eggemann@arm.com, rostedt@goodmis.org,
	bsegall@google.com, mgorman@suse.de, vschneid@redhat.com,
	kprateek.nayak@amd.com, luca.abeni@santannapisa.it,
	tommaso.cucinotta@gmail.com, mathieu.poirier@linaro.org,
	longman@redhat.com, ridong.chen@linux.dev, tj@kernel.org,
	hannes@cmpxchg.org, mkoutny@suse.com, shuah@kernel.org,
	bigeasy@linutronix.de, clrkwllms@kernel.org,
	parri.andrea@gmail.com, m.szyprowski@samsung.com,
	kayracizmeci@gmail.com, weihengqiuu@gmail.com,
	linux-kernel@vger.kernel.org, cgroups@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-rt-devel@lists.linux.dev,
	oleg@redhat.com, brauner@kernel.org, stable@vger.kernel.org
Subject: [PATCH v13 31/36] sched/deadline: Count a task that departs while a rebuild walks the tasks
Date: Tue,  1 Dec 2026 12:44:25 -0500	[thread overview]
Message-ID: <d08ae422166abcc534d0d4d081e19f4e899996f3.1796146461.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1796146461.git.mathieu.desnoyers@efficios.com>

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@efficios.com>
Cc: stable@vger.kernel.org
---
 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


  parent reply	other threads:[~2026-12-01 17:44 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 17:43 [PATCH v13 00/36] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-12-01 17:43 ` [PATCH v13 01/36] sched/deadline: Hold a reference on the deadline donor of a boosted task Mathieu Desnoyers
2026-12-01 17:43 ` [PATCH v13 02/36] sched/deadline: Replenish a throttled task boosted while asleep Mathieu Desnoyers
2026-12-01 17:43 ` [PATCH v13 03/36] sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub() Mathieu Desnoyers
2026-12-01 17:43 ` [PATCH v13 04/36] sched/deadline: Count the 0-lag time from the next period Mathieu Desnoyers
2026-12-01 17:43 ` [PATCH v13 05/36] sched/deadline: Keep a departing constrained task until its " Mathieu Desnoyers
2026-12-01 18:17   ` sashiko-bot
2026-12-01 17:44 ` [PATCH v13 06/36] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-12-01 18:20   ` sashiko-bot
2026-12-01 17:44 ` [PATCH v13 07/36] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 08/36] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 09/36] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 10/36] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 11/36] selftests/sched: Test the root domain rebuild in deadline_bw_test Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 12/36] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 13/36] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 14/36] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 15/36] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 16/36] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 17/36] selftests/sched: Test cpuset moves in deadline_bw_test Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 18/36] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-12-01 19:11   ` sashiko-bot
2026-12-01 17:44 ` [PATCH v13 19/36] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 20/36] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-12-01 19:02   ` sashiko-bot
2026-12-01 17:44 ` [PATCH v13 21/36] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 22/36] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-12-01 19:29   ` sashiko-bot
2026-12-01 17:44 ` [PATCH v13 23/36] selftests/sched: Test the root domain of a task in deadline_bw_test Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 24/36] sched/deadline: Count only active CPUs when spreading extra_bw Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 25/36] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 26/36] sched/deadline: Cap the runtime left of a constrained task at dl_runtime Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 27/36] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 28/36] sched/deadline: Record the start of the period Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 29/36] sched/deadline: Keep the debt of a task across a change of parameters Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 30/36] selftests/sched: Test parameter changes in deadline_bw_test Mathieu Desnoyers
2026-12-01 19:34   ` sashiko-bot
2026-12-01 17:44 ` Mathieu Desnoyers [this message]
2026-12-01 19:41   ` [PATCH v13 31/36] sched/deadline: Count a task that departs while a rebuild walks the tasks sashiko-bot
2026-12-01 17:44 ` [PATCH v13 32/36] sched/deadline: Count again a departed task that a boost brings back Mathieu Desnoyers
2026-12-01 19:47   ` sashiko-bot
2026-12-01 17:44 ` [PATCH v13 33/36] sched/deadline: Keep a reaped deadline task in a rebuild Mathieu Desnoyers
2026-12-01 19:57   ` sashiko-bot
2026-12-01 17:44 ` [PATCH v13 34/36] sched/deadline: Warn when a runqueue keeps the bandwidth of a task Mathieu Desnoyers
2026-12-01 17:44 ` [PATCH v13 35/36] selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test Mathieu Desnoyers
2026-12-01 19:51   ` sashiko-bot
2026-12-01 17:44 ` [PATCH v13 36/36] selftests/sched: Share the helpers of the deadline tests Mathieu Desnoyers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d08ae422166abcc534d0d4d081e19f4e899996f3.1796146461.git.mathieu.desnoyers@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=bigeasy@linutronix.de \
    --cc=boqun@kernel.org \
    --cc=brauner@kernel.org \
    --cc=bsegall@google.com \
    --cc=cgroups@vger.kernel.org \
    --cc=clrkwllms@kernel.org \
    --cc=dietmar.eggemann@arm.com \
    --cc=hannes@cmpxchg.org \
    --cc=juri.lelli@redhat.com \
    --cc=kayracizmeci@gmail.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=longman@redhat.com \
    --cc=luca.abeni@santannapisa.it \
    --cc=m.szyprowski@samsung.com \
    --cc=mathieu.poirier@linaro.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=parri.andrea@gmail.com \
    --cc=peterz@infradead.org \
    --cc=ridong.chen@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tglx@kernel.org \
    --cc=tj@kernel.org \
    --cc=tommaso.cucinotta@gmail.com \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=weihengqiuu@gmail.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®