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, anna-maria@linutronix.de, frederic@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 v14 34/39] sched/deadline: Count a task that departs while a rebuild walks the tasks
Date: Thu,  3 Dec 2026 09:58:48 -0500	[thread overview]
Message-ID: <24df95552195c057ff0b0b95a0245bad8d27dd5b.1796296313.git.mathieu.desnoyers@efficios.com> (raw)
In-Reply-To: <cover.1796296313.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. 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@efficios.com>
Cc: stable@vger.kernel.org
---
 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


  parent reply	other threads:[~2026-12-03 15:00 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 14:58 [PATCH v14 00/39] sched/deadline: bandwidth accounting fixes and selftests Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 01/39] sched/deadline: Hold a reference on the deadline donor of a boosted task Mathieu Desnoyers
2026-12-03 15:44   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 02/39] sched/deadline: Replenish a throttled task boosted while asleep Mathieu Desnoyers
2026-12-03 15:29   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 03/39] sched/deadline: Do not truncate a bandwidth to 32 bits in __dl_add/sub() Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 04/39] sched/deadline: Count the 0-lag time from the next period Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 05/39] sched/deadline: Clear dl_overrun under the rq lock Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 06/39] sched/deadline: Admit a returning task without the bandwidth it holds Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 07/39] sched/deadline: Leave an active task alone in the inactive timer Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 08/39] sched/deadline: Keep the deadline of a departed constrained task Mathieu Desnoyers
2026-12-03 15:45   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 09/39] sched/deadline: Keep departing bandwidth across a root domain rebuild Mathieu Desnoyers
2026-12-03 15:49   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 10/39] sched/deadline: Rebuild the root domains' accounting from the task list Mathieu Desnoyers
2026-12-03 15:57   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 11/39] sched/deadline: Record the root domain holding a task's bandwidth Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 12/39] sched/deadline: Leave a dying task out of a rebuild of the accounting Mathieu Desnoyers
2026-12-03 16:20   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 13/39] selftests/sched: Test SCHED_DEADLINE bandwidth across a root domain rebuild Mathieu Desnoyers
2026-12-03 16:10   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 14/39] selftests/sched: Test the root domain rebuild in deadline_bw_test Mathieu Desnoyers
2026-12-03 16:10   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 15/39] sched/deadline: Move a task's bandwidth only where room was made Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 16/39] cgroup/cpuset: Wait for an attach before rebuilding the sched domains Mathieu Desnoyers
2026-12-03 16:22   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 17/39] sched: Make deadline policy and affinity changes wait for a cpuset attach Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 18/39] cgroup/cpuset: Give back the bandwidth an attach did not move Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 19/39] cgroup/cpuset: Reserve the bandwidth in the root domain a task moves to Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 20/39] selftests/sched: Test cpuset moves in deadline_bw_test Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 21/39] sched/deadline: Make a rebuild count a task in the root domain it runs in Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 22/39] sched/deadline: Move a task's bandwidth to " Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 23/39] sched/deadline: Keep a task in the root domain that counts its bandwidth Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 24/39] sched/deadline: Keep the last CPU a task has in its root domain Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 25/39] cgroup/cpuset: Refuse to take a deadline task out of " Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 26/39] selftests/sched: Test the root domain of a task in deadline_bw_test Mathieu Desnoyers
2026-12-03 17:25   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 27/39] sched/deadline: Count only active CPUs when spreading extra_bw Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 28/39] sched/deadline: Cancel the replenishment timer with the parameters Mathieu Desnoyers
2026-12-03 17:39   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 29/39] sched/deadline: Cap the runtime left of a constrained task at dl_runtime Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 30/39] sched/deadline: Check the runtime of a task that comes back runnable Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 31/39] sched/deadline: Record the start of the period Mathieu Desnoyers
2026-12-03 17:42   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 32/39] sched/deadline: Keep the debt of a task across a change of parameters Mathieu Desnoyers
2026-12-03 14:58 ` [PATCH v14 33/39] selftests/sched: Test parameter changes in deadline_bw_test Mathieu Desnoyers
2026-12-03 17:56   ` sashiko-bot
2026-12-03 14:58 ` Mathieu Desnoyers [this message]
2026-12-03 14:58 ` [PATCH v14 35/39] sched/deadline: Count again a departed task that a boost brings back Mathieu Desnoyers
2026-12-03 18:01   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 36/39] sched/deadline: Keep a reaped deadline task in a rebuild Mathieu Desnoyers
2026-12-03 18:14   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 37/39] sched/deadline: Warn when a runqueue keeps the bandwidth of a task Mathieu Desnoyers
2026-12-03 18:05   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 38/39] selftests/sched: Test PI against hotplug and rebuilds in deadline_bw_test Mathieu Desnoyers
2026-12-03 18:07   ` sashiko-bot
2026-12-03 14:58 ` [PATCH v14 39/39] 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=24df95552195c057ff0b0b95a0245bad8d27dd5b.1796296313.git.mathieu.desnoyers@efficios.com \
    --to=mathieu.desnoyers@efficios.com \
    --cc=anna-maria@linutronix.de \
    --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=frederic@kernel.org \
    --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®