From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=158.69.130.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796147075; b=dul8cMjxGJy20JBHF/vMoasCyjvtQ2druvUqMUmVzXcjhgamLjPCW10TuYde4N8Xg63u vAr6xrOBbNv5bTYPq5EqEWKXdoaXRm42cSuHrSTsxwSsivS/zpKHpdGiEFe/IJAZmBKWV zvq1ptVYX/kbVXnyzvifN+hNrqHuSSid7vW/fhxvd4jRPmdOiz84kVghX8+9/AFHZEZ6S RvJFutsui48sRJ61bkNfSQFFFeg21EoBa33ArOumGBWG7oJ3pZm7cOyvt6oEo9pXOrp8e uqZJCofRDqs0ETNy6M4kkrKrt7thLajYL6JU5gUGangqTB7rVwFSizJyCbrogJ1T30A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796147075; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=ng96vELL4VFUn0HJxxE6nwJXOAukoGBBRZBHeQ3x4Rw=; b=qT8Gpo1w6+5SNWM2w8+DI+SH7UVnrr8NDlQyzfKFqNrk0RPpMaN+a/f/S4PFfZvi3/3Y aoDEzaorSM7z1QU4CMhO0x5YMviDRKA0O+2W6p/J+xvNfN50h42GMualQ65bUFNyBFyQl aIrOVuJpJeW9Lt7Iw9Zdam3ZGexDwlPoLE/T053RNFE4P3Xr1v5Qgk1DxAtrffDVjbGif eiOL2qV9kClQfzw5mMrA95St5VeiszzVqIQ1EvTIYAxzi2teac133CQJLfPOMe3c/lo2n 5F/Cooe1IPonjTi1VNHRHVLWewgY8NtfAZ4Zkyp1hTbOGxWwkpBPQ0dGdstmka2rawQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=efficios.com; spf=pass smtp.mailfrom=efficios.com; dkim=pass header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=OaPahqK9; arc=none smtp.remote-ip=158.69.130.18 Received: from smtpout.efficios.com (smtpout.efficios.com [158.69.130.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBE10606A32F; Tue, 1 Dec 2026 17:44:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CBE10606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=efficios.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=efficios.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.a=rsa-sha256 header.s=smtpout1 header.b=OaPahqK9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796147074; bh=ng96vELL4VFUn0HJxxE6nwJXOAukoGBBRZBHeQ3x4Rw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OaPahqK9CeBRG250FZhV+m4A99wdpq44ouK0zYyHl1HVrVPU7bc/JuaQc43Xp6QQP te+/tGWNXsM1g0wBLiQXBVFJQwaqtpRnGuLFttchAy06U8AERNkSIpZrUbqeg9Cs0o sPY2DSR4iIbgjbtp6sEEdiusL+8RFhVFNuE68FrEkGCjB/OemAZ5HSBv0BTCwKQqNH Mb6EoweOC6RYQ28m7VjfCdrs4dVtTrZrmj41XUs4MftntZTDMPPMYd34XE65PGqomq Q45AqmptTpUJpofv3XgQnQWpnmEQmGmfJKWBuExGGmhDiul7LVLOKadfmKvxUi6pSr jOfkmppmeyW3g== Received: from thinkos.internal.efficios.com (mtl.efficios.com [216.120.195.104]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jf7Ys6f16zZ4M; Tue, 01 Dec 2026 12:44:33 -0500 (EST) From: Mathieu Desnoyers To: juri.lelli@redhat.com, peterz@infradead.org, mingo@redhat.com, vincent.guittot@linaro.org Cc: Mathieu Desnoyers , 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 04/36] sched/deadline: Count the 0-lag time from the next period Date: Tue, 1 Dec 2026 12:43:58 -0500 Message-ID: <13165145b0317cf69aa236d1a34f3532b94ebe73.1796146461.git.mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A deadline task that blocks keeps counting in running_bw until its 0-lag time. Commit 209a0cbda7a0 ("sched/deadline: Improve the tracking of active utilization") added this for GRUB. A task that leaves the class or exits also keeps its bandwidth in total_bw until then. Commit 387e31300b57 ("sched/deadline: Fix the update of the total -deadline utilization") added that. The 0-lag time is when the task, served at its bandwidth since the start of its period, has received the runtime it used. That is: next period - runtime * dl_period / dl_runtime Here runtime is the runtime left. GRUB [1] has the same formula (rule 5 of section 4.1), with the deadline in place of the next period. In GRUB, the deadline is always the end of the period (rules 1 and 3). For a constrained deadline, rule R6 of [2] puts the end of the period at deadline - dl_deadline + dl_period. This is what dl_next_period() returns. dl_time_to_zerolag() uses the deadline in place of the next period. The two are the same only for an implicit deadline. A constrained deadline comes dl_period - dl_deadline before the next period. So the 0-lag time comes that much too early. For example, take a task with a runtime of 10ms, a deadline of 20ms and a period of 500ms. It runs 2ms into its period, then sleeps with 8ms left. dl_time_to_zerolag() puts its 0-lag time at 20 - 8 * 500 / 10 = -380ms, before the task even blocked. Counted from the next period, it is 500 - 400 = 100ms. On v7.4-rc2, this would affect a constrained task: - When it blocks, GRUB reclaiming would give its bandwidth to SCHED_FLAG_RECLAIM tasks too early. Schedutil could lower the CPU frequency too early. Deadline tasks could then miss their deadlines. - When it exits or leaves the class, admission control could admit other tasks into a temporary overload. This was found by reading. Fix this by counting the 0-lag time from the next period, with dl_next_period(). Update the formula in Documentation/scheduler/sched-deadline.rst too. A task with no period yet has a deadline of 0. dl_next_period() then returns dl_period - dl_deadline, which is above 0 for a constrained deadline. Right after boot, rq_clock can still be below that value. Counted from that next period, the 0-lag time would be ahead. Make dl_time_to_zerolag() return no 0-lag time for such a task. dl_check_constrained_dl() has the same problem since commit df8eac8cafce ("sched/deadline: Throttle a constrained deadline task activated after the deadline"). It sees a deadline of 0 as passed, and the next period as ahead. So a task that enters the class with a constrained deadline right after boot would be throttled until rq_clock reaches dl_period - dl_deadline. Make dl_check_constrained_dl() skip a deadline of 0 too. This needs "sched/deadline: Fix an overflow of the 0-lag time with long periods", which adds dl_time_to_zerolag(). A task that leaves the class and comes back after its 0-lag time starts a new period. "sched/deadline: Reset the deadline and the runtime with the parameters" does this. So a constrained task can come back after the early 0-lag time, before its next period. It then runs again before that next period. Three cases of deadline_bw_test check this: reenter_runnable_in_gap, reenter_asleep_in_gap and reenter_pi_blocked_in_gap. "selftests/sched: Test parameter changes in deadline_bw_test", later in this series, adds them. They fail without this patch and pass from it on. [1] L. Abeni, G. Lipari, A. Parri, Y. Sun, "Parallel and sequential reclaiming in multicore real-time global scheduling", arXiv 1512.01984, 2016, https://arxiv.org/abs/1512.01984 [2] D. Casini, L. Abeni, A. Biondi, T. Cucinotta, G. Buttazzo, "Constant Bandwidth Servers with Constrained Deadlines", RTNS 2017, https://retis.santannapisa.it/~a.biondi/papers/CBS_CD_RTNS17.pdf Fixes: 209a0cbda7a0 ("sched/deadline: Improve the tracking of active utilization") Fixes: df8eac8cafce ("sched/deadline: Throttle a constrained deadline task activated after the deadline") Signed-off-by: Mathieu Desnoyers Reviewed-by: Juri Lelli Cc: stable@vger.kernel.org --- Documentation/scheduler/sched-deadline.rst | 12 ++++--- kernel/sched/deadline.c | 39 +++++++++++++++------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/Documentation/scheduler/sched-deadline.rst b/Documentation/scheduler/sched-deadline.rst index 9019b66f6a5b..3529b667c07f 100644 --- a/Documentation/scheduler/sched-deadline.rst +++ b/Documentation/scheduler/sched-deadline.rst @@ -171,12 +171,14 @@ Deadline Task Scheduling The 0-lag time for a task entering the ActiveNonContending state is computed as:: - (runtime * dl_period) - deadline - --------------------- - dl_runtime + (runtime * dl_period) + next period - --------------------- + dl_runtime - where runtime is the remaining runtime, while dl_runtime and dl_period - are the reservation parameters. + where runtime is the remaining runtime, next period is the start of + the task's next period, and dl_runtime and dl_period are the + reservation parameters. When dl_deadline equals dl_period, the next + period starts at the deadline. (b) If the task wakes up before the inactive timer fires, the task re-enters the ActiveContending state and the "inactive timer" is canceled. diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index a12b26b94642..a02975eb91f6 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -375,20 +375,24 @@ static void dl_change_utilization(struct task_struct *p, u64 new_bw) dl_rq_change_utilization(task_rq(p), &p->dl, new_bw); } +static inline u64 dl_next_period(struct sched_dl_entity *dl_se); static void __dl_clear_params(struct sched_dl_entity *dl_se); /* * Return the time from now to the 0-lag time of @dl_se, or a negative * value if the 0-lag time has passed. * - * The 0-lag time is when a task running at its reserved bandwidth would - * use up the runtime it has left: + * The 0-lag time is when the task, served at its reserved bandwidth + * since the start of its period, has received the runtime it used. From + * there to the next period, the bandwidth gives exactly the runtime left: * - * 0-lag = deadline - runtime * dl_period / dl_runtime + * 0-lag = next period - runtime * dl_period / dl_runtime * * Example: 30ms every 100ms, blocking 10ms into its period with 20ms - * left. 0-lag = 100 - 20 * 100 / 30 = 33ms. From there to the deadline, - * 30% of the CPU is exactly the 20ms left. + * left. 0-lag = 100 - 20 * 100 / 30 = 33ms. + * + * Do not count from the deadline. A constrained deadline comes before + * the next period, so the 0-lag time would come too early. * * The product can overflow 64 bits, so mul_u64_u64_div_u64() multiplies * on 128 bits. Its quotient must still fit: on x86-64 it traps @@ -400,12 +404,16 @@ static void __dl_clear_params(struct sched_dl_entity *dl_se); */ static s64 dl_time_to_zerolag(struct sched_dl_entity *dl_se, struct rq *rq) { - /* Time to the deadline. Negative if the deadline has passed. */ - s64 left = dl_se->deadline - rq_clock(rq); + /* Time to the next period. Negative if it has started. */ + s64 left = dl_next_period(dl_se) - rq_clock(rq); + + /* A task with no period yet has received nothing: no 0-lag time. */ + if (!dl_se->deadline) + return -1; if (dl_se->runtime < 0) { /* - * Overrun: the 0-lag time is after the deadline, by the time + * Overrun: the 0-lag time is after the next period, by the time * needed to pay the overrun back at the reserved bandwidth. */ u64 overrun = -dl_se->runtime; @@ -426,8 +434,8 @@ static s64 dl_time_to_zerolag(struct sched_dl_entity *dl_se, struct rq *rq) } /* - * Runtime left: the 0-lag time is before the deadline. It has - * passed if the deadline has. + * Runtime left: the 0-lag time is before the next period. It has + * passed if the next period has started. */ if (left < 0) return -1; @@ -435,7 +443,7 @@ static s64 dl_time_to_zerolag(struct sched_dl_entity *dl_se, struct rq *rq) /* * runtime * dl_period / dl_runtime > left: the 0-lag time has * passed. The runtime left is more than the bandwidth gives until - * the deadline, as a wakeup would find with dl_entity_overflow(). + * the next period. */ if (dl_se->runtime > mul_u64_u64_div_u64(left, dl_se->dl_runtime, dl_se->dl_period)) { @@ -1468,7 +1476,14 @@ static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se) { struct rq *rq = rq_of_dl_se(dl_se); - if (dl_time_before(dl_se->deadline, rq_clock(rq)) && + /* + * A deadline of 0 means the task has no period yet: this enqueue + * starts one. Skip it, or the check below would see a deadline in + * the past. While rq_clock is still below dl_period - dl_deadline + * (right after boot), it would then throttle the task until a next + * period computed from that 0. + */ + if (dl_se->deadline && dl_time_before(dl_se->deadline, rq_clock(rq)) && dl_time_before(rq_clock(rq), dl_next_period(dl_se))) { if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(dl_se))) return; -- 2.47.3