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=1796147084; b=ECn5OcYOWGcZWSmDhVUNTqWF7t9MWJcyPeflCBuE9DERq3kqEO6q5eiJ8v5/aMghAJbp 9IguMJSNls9Qi9BPACknDjiPuafFRlbWLJhsNbGw0ys2cEsBvXxvau9zDZhlicMmezDTa hAWFD6OyhIcvuH3c6C8hvP3tDte8j3ZW7Vv56Qy6TgVzNFApnDuJpoyeiFrxZQc/81+UM OZc79qI3jMRVVcE2n7YvOpCle5AQaiDXrh5QJWUDjBkjtRscvdyrfe4mlgvJmnv51MNb8 Dx/c8DKFk9z3/0WtHH99I+kGoV7pBPScyJzbAWCqO3AZN01cvP8PZY5ZV5MW16CC/Kg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796147084; 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=kd/R4cuAKtbzPciuZ96mwolybv8830CzvDBsk+nZ/1Y=; b=R0BVusJIh0NgCWj/xNja2/kshIgoj/Nhyqls/1OJj7K4WQ6GlwCWQ9Xn+aMNowILPBYj JNFDR4vTT9IUmxgQPYXesRH74SsfwyVaHHz5LnRhJ87YUQby86CocUOjTzF0OIcLb2/bM nHMq0+82Xy+BBCMO0ttpfVq8xLUiUuSfHMQSTu/JYqnZOL2V3LuEQiGb2QTcMZnbkoAbG sdewbsz1AHrqy13Cbmu9UfWfH/g0sW2b/EV7VXn510PHZfxsZQXAcv2uXIEOTndk7yXpf 8G3KoulLYjYrqQrBud3WjUMAyZ8sz4Mv1yxZw07d7uxg8p7FIf/rt0iv1JaXOUjpWuw== 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=vahgc9n2; 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 73A6E60A6F5B; Tue, 1 Dec 2026 17:44:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 73A6E60A6F5B 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=vahgc9n2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=smtpout1; t=1796147078; bh=kd/R4cuAKtbzPciuZ96mwolybv8830CzvDBsk+nZ/1Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vahgc9n20z6V4IR4upPG5q9Gp08+DB5s8V4nCwPInUgsLn1m4axITvm4qEoVBk1UG +R1I1Q20WapvXXiwdFk/P7V/tg0FYZE9jpJSIoLq0mdnDINYOsH5ZsK8x4Iqg3aIU+ 4NBOs8wjyRcRg4SV/X4tMLR4iPiP44O31MjUnC5potuQT7WSa35/nYFK0oD4m1RbBS q24SABwVLieWI/JLOFYZHV2IsHrdW9gy9wRG1Qz6b9HOlE7oOBK3OWP1surgHs7t88 1n7vJ/OBLa75Y5TfWpEuX3qSAoC+GnHTnXbXCrzn1fFp1ofHINWJCKNIh8AZZR2Wuz XdS/1GWWRlUYQ== Received: from thinkos.internal.efficios.com (mtl.efficios.com [216.120.195.104]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4jf7Yy3cLRzYwC; Tue, 01 Dec 2026 12:44:38 -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 29/36] sched/deadline: Keep the debt of a task across a change of parameters Date: Tue, 1 Dec 2026 12:44:23 -0500 Message-ID: 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 sched_setattr() changes the parameters of a deadline task, and leaves the period in progress as it is. Commit 40767b0dc768 ("sched/deadline: Fix deadline parameter modification handling") chose this: a change that takes effect at once "would allow one to circumvent the CBS guarantees". It also knew the cost: admission control counts the new bandwidth at once, which is "not on the safe side". The period in progress does not match the new parameters: - A task that shrinks its reservation runs on with its old runtime, while other tasks can be admitted into the bandwidth it gave back. - The replenishment timer of a throttled task stays armed for the old dl_period. - dl_next_period() gives a wrong next period after a change of dl_deadline. It comes from commit 5ac69d37784b ("sched/deadline: Make sure the replenishment timer fires in the next period"). A task can then get more than its bandwidth, and other deadline tasks can miss their deadlines. Tang, Anderson and Abeni build such an overload with two tasks of 1ms every 100ms [1]. Each task in turn is raised to 70ms before its next period, and cut back right after its refill. It keeps the job it just got, so both tasks run jobs of 70ms. Admission control counts 71% of the CPU for the two tasks. On v7.4-rc2, they take 99% of the CPU in the test case params_swap_overload. With this series, they get 64%. The paper does not allow changes of parameters. But sched_setattr() allows them: that is user-space ABI. Fix this by fitting the period in progress to the new parameters, at the change. A task served at its old bandwidth since the start of its period would have received some runtime by now. If it used more, the difference is its debt. The new dl_setattr_period(), called from __setparam_dl(), keeps that debt: - An awake task goes on with its job, with the runtime left, up to the new dl_runtime. - Its next period starts when the new bandwidth has repaid the debt and supplied that runtime. A task that must wait is throttled until then. - With a new constrained deadline, a job that ran counts as a whole job of the new parameters. dl_setattr_place() lists the rules. The rules need the start of the period, period_start, which the previous patch records. dl_next_period() and the replenishment now use it. dl_task_timer() no longer refills a task before its next period. A change can move that period while the handler waits for the rq lock. With this patch, sched_setattr() can leave a sleeping task no runtime, before its deadline. Once woken, the task would run until the next tick. So enqueue_dl_entity() now throttles it until its next period, as update_curr_dl_se() would. The task can then reach the cancel of the replenishment timer at the end of enqueue_dl_entity(). Only servers reached it before. hrtimer_try_to_cancel() there does not drop the reference that a queued timer holds on a task, so the task_struct would leak. Use cancel_replenish_timer() there, which drops it. Some tasks are not fitted at the change. The new flag dl_new_params marks them: - A task with no period in progress: its next enqueue checks it, as a wakeup does. A task that waits on an rt_mutex is checked at once, in the new dl_check_blocked_params(). The chain walk copies its deadline into its waiter node before any wakeup. With a deadline that the check would move later, the waiter would sort before waiters with earlier deadlines. - A boosted task, which runs on the reservation of its donor. Its first enqueue after the deboost checks it, as a wakeup does. - A task that comes back to the class with the same parameters, before its 0-lag time. It still has its old runtime and deadline, and __setparam_dl() fits nothing. __setscheduler_params() marks it. User-space sees two changes: - New parameters apply to the period in progress. A task in debt waits until the debt is repaid. - A task in debt that leaves the class stays counted by admission control until then. Meanwhile, sched_setattr() can refuse another task with -EBUSY. sched_getattr() returns what it returned before. These cases remain: - Admission control still releases the old bandwidth at the change. After a cut of the runtime, a task admitted into the released bandwidth would rarely miss a deadline close to the change. - With a constrained deadline, a job that already used the new dl_runtime ends at the change, even with old runtime left. - A task that changes its constrained dl_deadline back and forth while it waits moves its next period later each time. - A boosted task keeps no debt. As on v7.4-rc2, the check after its deboost can start a new period at once. The Sashiko AI review of this series found the dl_next_period() problem. "selftests/sched: Test parameter changes in deadline_bw_test", later in this series, lists the cases that fail without this patch. This patch also updates params_switched_asleep, from "selftests/sched: Add a test of SCHED_DEADLINE bandwidth accounting" in the first series. The bandwidth now stays counted until the debt is repaid. [1] S. Tang, J. H. Anderson, L. Abeni, "On the Defectiveness of SCHED_DEADLINE w.r.t. Tardiness and Affinities, and a Partial Fix", RTNS 2021, section 3.1, Example 1, https://doi.org/10.1145/3453417.3453440 Fixes: 40767b0dc768 ("sched/deadline: Fix deadline parameter modification handling") Fixes: 5ac69d37784b ("sched/deadline: Make sure the replenishment timer fires in the next period") Signed-off-by: Mathieu Desnoyers Cc: stable@vger.kernel.org --- include/linux/sched.h | 8 + kernel/sched/deadline.c | 392 +++++++++++++++++- kernel/sched/sched.h | 2 + kernel/sched/syscalls.c | 23 +- .../sched/deadline/deadline_bw_test.c | 28 +- 5 files changed, 423 insertions(+), 30 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index ccd972ca44e4..4d2bf4de524d 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -720,6 +720,13 @@ struct sched_dl_entity { * @dl_bw_oncpu tells if the this_bw of a runqueue counts the task's * @dl_bw: add_rq_bw() and sub_rq_bw() set and clear it, under that * runqueue's lock. Not for servers, which keep @dl_bw_attached. + * + * @dl_new_params tells that the runtime left still needs a check + * against new parameters, as a wakeup does. sched_setattr() sets it + * when it changes the runtime, the deadline or the period, or brings + * the task back into the class. It stays set while the task is + * boosted, or when the task had no period in progress: see + * dl_setattr_period(). */ unsigned int dl_throttled : 1; unsigned int dl_yielded : 1; @@ -733,6 +740,7 @@ struct sched_dl_entity { unsigned int dl_defer_idle : 1; unsigned int dl_bw_attached : 1; unsigned int dl_bw_oncpu : 1; + unsigned int dl_new_params : 1; /* * Bandwidth enforcement timer. Each -deadline task has its diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c index 5cc4f36c5146..7aa6c3f7e99d 100644 --- a/kernel/sched/deadline.c +++ b/kernel/sched/deadline.c @@ -1249,6 +1249,7 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se) u64 runtime = READ_ONCE(pi_se->dl_runtime); u64 deadline = READ_ONCE(pi_se->dl_deadline); u64 period = READ_ONCE(pi_se->dl_period); + bool new_period = false; /* * A boosted entity runs on the parameters of its donor, the top @@ -1309,7 +1310,18 @@ static void replenish_dl_entity(struct sched_dl_entity *dl_se) dl_se->deadline += period; dl_se->period_start += period; dl_se->runtime += runtime; + new_period = true; } + /* + * The loop moves the deadline and period_start together. After + * sched_setattr(), deadline - period_start can differ from + * dl_deadline. Set the deadline from period_start for the new + * period. A boosted task runs on the parameters of its donor: + * leave its deadline as the loop moved it. sched_setattr() does + * not change a dl_server. + */ + if (new_period && !is_dl_boosted(dl_se) && !dl_server(dl_se)) + dl_se->deadline = dl_se->period_start + dl_se->dl_deadline; /* * At this point, the deadline really should be "in @@ -1499,7 +1511,9 @@ static void update_dl_entity(struct sched_dl_entity *dl_se) * that allows more than dl_runtime. So cap the runtime at dl_runtime * first. An implicit task is checked against its bandwidth, which * keeps it within its reservation for any deadline. A boosted task - * runs on the parameters of its donor. + * runs on the parameters of its donor. dl_setattr_period() leaves a + * task boosted at the change as it is: its first enqueue after the + * deboost needs this cap. */ if (!dl_is_implicit(dl_se) && !is_dl_boosted(dl_se) && dl_se->runtime > (s64)dl_se->dl_runtime) @@ -1535,9 +1549,29 @@ static void update_dl_entity(struct sched_dl_entity *dl_se) } } +/* + * Return the start of the next period of @dl_se. Do not work it out from + * the deadline and dl_deadline: sched_setattr() can change dl_deadline + * during the period. + */ static inline u64 dl_next_period(struct sched_dl_entity *dl_se) { - return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period; + return dl_se->period_start + dl_se->dl_period; +} + +/* + * Return true if @dl_se is in its constrained gap. Its deadline is + * constrained, it has started a period, and the rq clock is between its + * deadline and its next period. An enqueue then throttles it until that + * period. + */ +static inline bool dl_in_constrained_gap(struct sched_dl_entity *dl_se) +{ + u64 now = rq_clock(rq_of_dl_se(dl_se)); + + return dl_se->deadline && !dl_is_implicit(dl_se) && + dl_time_before(dl_se->deadline, now) && + dl_time_before(now, dl_next_period(dl_se)); } /* @@ -1575,7 +1609,7 @@ static int start_dl_timer(struct sched_dl_entity *dl_se) WARN_ON_ONCE(!dl_se->dl_throttled); act = ns_to_ktime(dl_se->deadline - dl_se->runtime); } else { - /* act = deadline - rel-deadline + period */ + /* act = the start of the next period */ act = ns_to_ktime(dl_next_period(dl_se)); } @@ -1725,6 +1759,10 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) if (dl_se->dl_throttled && !is_dl_boosted(dl_se)) { sched_clock_tick(); update_rq_clock(rq); + /* Not before the next period: see below. */ + if (dl_time_before(rq_clock(rq), dl_next_period(dl_se)) && + start_dl_timer(dl_se)) + goto unlock; replenish_dl_entity(dl_se); } goto unlock; @@ -1750,6 +1788,18 @@ static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) sched_clock_tick(); update_rq_clock(rq); + /* + * sched_setattr() can move the next period while this handler waits + * for the rq lock: see dl_setattr_period(). Do not refill before that + * period starts. start_dl_timer() arms the timer there, or finds it + * armed. If it finds the time passed, refill now. A boosted task + * asleep is in a period of its donor: refill it as before. + */ + if (!is_dl_boosted(dl_se) && + dl_time_before(rq_clock(rq), dl_next_period(dl_se)) && + start_dl_timer(dl_se)) + goto unlock; + /* * If the throttle happened during sched-out; like: * @@ -2925,7 +2975,18 @@ static void __dequeue_dl_entity(struct sched_dl_entity *dl_se) static void enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags) { + bool new_params = dl_se->dl_new_params; + WARN_ON_ONCE(on_dl_rq(dl_se)); + /* + * Clear dl_new_params unless the task is boosted. The code below + * checks the runtime left as a wakeup does, or refills the task. + * A throttled task waits for its replenishment instead. A boosted + * task runs on its donor's reservation: its first enqueue after + * the deboost checks it. + */ + if (!is_dl_boosted(dl_se)) + dl_se->dl_new_params = 0; update_stats_enqueue_dl(dl_rq_of_se(dl_se), dl_se, flags); @@ -2974,12 +3035,13 @@ enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags) update_dl_entity(dl_se); } else if (flags & ENQUEUE_REPLENISH) { replenish_dl_entity(dl_se); - } else if ((flags & ENQUEUE_CLASS) && !is_dl_boosted(dl_se)) { + } else if (((flags & ENQUEUE_CLASS) || new_params) && + !is_dl_boosted(dl_se)) { /* - * The task enters the class runnable. It may come back before - * its deadline, with the runtime left from older parameters. - * Check that runtime against the new parameters, as a wakeup - * does. + * The task enters the class, or its parameters change, while + * it is runnable. It may have runtime left from older + * parameters. Check that runtime against the new parameters, + * as a wakeup does. */ update_dl_entity(dl_se); } else if ((flags & ENQUEUE_MOVE) && @@ -2988,6 +3050,22 @@ enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags) setup_new_dl_entity(dl_se); } + /* + * A task can be woken with no runtime left, before its deadline, after + * sched_setattr() left it none. It would run until the next tick. + * Throttle it now, until its next period, as update_curr_dl_se() + * would. + */ + if (!dl_server(dl_se) && !is_dl_boosted(dl_se) && + !dl_entity_is_special(dl_se) && dl_se->dl_runtime && + !dl_se->dl_throttled && dl_se->runtime <= 0) { + struct rq *rq = rq_of_dl_se(dl_se); + + dl_se->dl_throttled = 1; + if (task_current_donor(rq, dl_task_of(dl_se))) + resched_curr(rq); + } + /* * If the reservation is still throttled, e.g., it got replenished but is a * deferred task and still got to wait, don't enqueue. @@ -3002,7 +3080,7 @@ enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags) * Also cancel earlier timers, since letting those run is pointless. */ if (dl_se->dl_throttled) { - hrtimer_try_to_cancel(&dl_se->dl_timer); + cancel_replenish_timer(dl_se); dl_se->dl_defer_armed = 0; dl_se->dl_throttled = 0; } @@ -5089,19 +5167,280 @@ int sched_dl_overflow(struct task_struct *p, int policy, return err; } +/* Return true if @p waits on an rt_mutex. The caller holds its pi_lock. */ +static inline bool dl_task_pi_blocked(struct task_struct *p) +{ +#ifdef CONFIG_RT_MUTEXES + return p->pi_blocked_on; +#else + return false; +#endif +} + /* - * This function initializes the sched_dl_entity of a newly becoming - * SCHED_DEADLINE task. + * Return @a * @b / @c rounded up, capped at @cap. With a raised + * sched_deadline_period_max_us, a runtime times a ratio of period to + * runtime can exceed 64 bits. mul_u64_u64_div_u64() then traps on + * x86-64, so compare with @cap first. + */ +static u64 dl_mul_div_cap(u64 a, u64 b, u64 c, u64 cap) +{ + if (b > c && a > mul_u64_u64_div_u64(cap, c, b)) + return cap; + return min(mul_u64_u64_div_u64_roundup(a, b, c), cap); +} + +/* The longest wait that sched_setattr() sets, far longer than any period. */ +#define DL_SETATTR_WAIT_MAX (1ULL << 62) + +/* + * Fit a period of @dl_se to its new parameters. sched_setattr() changed + * them from @old_runtime, @old_deadline and @old_period. At @now, + * *@start, *@deadline and *@runtime describe the period in progress + * under the old parameters. On return, they describe a period under the + * new ones. Return true if the task must wait for that period, with no + * runtime. * - * Only the static values are considered here, the actual runtime and the - * absolute deadline will be properly calculated when the task is enqueued - * for the first time with its new policy. + * These terms are used below: + * - The release of the job is *@deadline - @old_deadline. + * - The 0-lag time is when the task, served at its old bandwidth since + * *@start, would have received the runtime it used. + * - The debt is the runtime that the old bandwidth gives from @now to + * a 0-lag time ahead. A task with a debt ran ahead of its + * reservation. + * - The repayment is when the new bandwidth, from @now, has supplied + * the debt and the runtime that the job keeps. + * + * Keep the debt across the change. Admission control already counts the + * new bandwidth, so repay the debt at that bandwidth. Otherwise, a change + * could give the task a new period at once, ahead of the tasks it was + * admitted with. + * + * If @run, the task goes on in its current job, with the runtime left + * capped at dl_runtime. The next period starts at the repayment. With a + * new implicit deadline, the job ends there too. + * + * A constrained deadline, old or new, adds these rules: + * - A job past its old deadline, or past its release plus dl_deadline, + * keeps no runtime. + * - A job keeps no more than the runtime left of a job of the new + * parameters. + * - Two releases are at least dl_period apart. + * + * A new constrained deadline adds these rules: + * - The job keeps no more runtime than the new density allows before + * its release plus dl_deadline. + * - The job ends at max(release + dl_deadline, + * min(repayment, old deadline)). + * - A job that ran counts as a whole job of the new parameters. So the + * next period starts no earlier than the end of the job - dl_deadline + * + dl_period. Otherwise, an old job followed closely by new jobs + * could put more demand in a window than either set of parameters + * does. + * + * Without a job to go on with, the task waits for its next period with no + * runtime, or starts it now if it is due. A task @asleep with a new + * constrained deadline does not wait: its wakeup starts its next period, + * or throttles it until then. Throttling it while it sleeps could waste + * a period. Its timer could start a period that ends before the wakeup. + * The wakeup would then throttle it until the period after. + */ +static bool dl_setattr_place(struct sched_dl_entity *dl_se, u64 now, + u64 *start, u64 *deadline, s64 *runtime, + u64 old_runtime, u64 old_deadline, u64 old_period, + bool run, bool asleep) +{ + u64 dl_runtime = dl_se->dl_runtime; + u64 dl_deadline = dl_se->dl_deadline; + u64 dl_period = dl_se->dl_period; + bool constrained = dl_deadline < dl_period || old_deadline < old_period; + u64 release = *deadline - old_deadline; + u64 zerolag = *start + old_period; + u64 debt = 0, keep = 0, used = 0, next = now, end = *deadline; + + /* Round against the task, here and below. */ + if (*runtime > 0) + zerolag -= mul_u64_u64_div_u64(*runtime, old_period, old_runtime); + else + zerolag += dl_mul_div_cap(-*runtime, old_period, old_runtime, + DL_SETATTR_WAIT_MAX); + if (dl_time_before(now, zerolag)) + debt = mul_u64_u64_div_u64_roundup(zerolag - now, old_runtime, old_period); + + if (*runtime < (s64)old_runtime) + used = old_runtime - max_t(s64, *runtime, 0); + + if (run && *runtime > 0) { + if (!constrained) { + keep = min_t(u64, *runtime, dl_runtime); + } else if (dl_time_before(now, *deadline) && + dl_time_before(now, release + dl_deadline)) { + if (used < dl_runtime) + keep = min_t(u64, *runtime, dl_runtime - used); + if (dl_deadline < dl_period) + keep = min(keep, mul_u64_u64_div_u64(release + dl_deadline - now, + dl_runtime, dl_deadline)); + } + } + + next += dl_mul_div_cap(debt + keep, dl_period, dl_runtime, DL_SETATTR_WAIT_MAX); + if (dl_deadline < dl_period && keep) { + if (dl_time_before(next, end)) + end = next; + if (dl_time_before(end, release + dl_deadline)) + end = release + dl_deadline; + } + if (constrained && dl_time_before(next, release + dl_period)) + next = release + dl_period; + /* A job that ran counts as a whole job: see above. */ + if (dl_deadline < dl_period && (keep || used) && + dl_time_before(next, end - dl_deadline + dl_period)) + next = end - dl_deadline + dl_period; + + if (keep) { + *runtime = keep; + *start = next - dl_period; + *deadline = dl_deadline < dl_period ? end : next; + return false; + } + *runtime = 0; + if (asleep && dl_deadline < dl_period) { + if (dl_time_before(next, now)) + next = now; + *start = next - dl_period; + *deadline = *start + dl_deadline; + return false; + } + if (!dl_time_before(now, next)) { + *start = now; + *deadline = now + dl_deadline; + *runtime = dl_runtime; + return false; + } + *start = next - dl_period; + *deadline = *start + dl_deadline; + return true; +} + +/* + * Fit the period in progress of @dl_se to its new parameters, by the + * rules of dl_setattr_place(). @old_runtime, @old_deadline and + * @old_period are the parameters before sched_setattr() changed them. + * A task that must wait for its next period is throttled until then. + * + * Treat a task blocked on an rt_mutex as awake. The chain walk copies + * its deadline into its waiter node before any wakeup, so that deadline + * must be right at once. + * + * A boosted task runs on the reservation of its donor. Leave its period + * as it is. + * + * Return true if the runtime left still needs the check that a wakeup + * does. This is the case for a task with no period in progress: its + * next enqueue checks it. It is also the case for a boosted task: its + * first enqueue after the deboost checks it. Return false for any + * other task: it is now in a period of the new parameters. The wakeup + * check rounds to the microsecond. It could find an overflow there and + * start a new period, which the debt does not allow. + * + * The caller holds the rq lock and the pi_lock of the task, and has + * updated the rq clock. The task is not on its dl_rq: every caller runs + * inside a change of the task, which dequeues it first. + */ +static bool dl_setattr_period(struct sched_dl_entity *dl_se, u64 old_runtime, + u64 old_deadline, u64 old_period) +{ + struct task_struct *p = dl_task_of(dl_se); + struct rq *rq = task_rq(p); + u64 now = rq_clock(rq); + bool asleep = !task_on_rq_queued(p) && !dl_task_pi_blocked(p); + + if (!dl_se->deadline || !dl_se->dl_runtime || !old_runtime || !old_period) + return true; + + if (is_dl_boosted(dl_se)) + return true; + + /* + * start_dl_timer() does not move a queued timer: cancel it first. If + * its handler is already running, the cancel fails. The handler then + * waits for the rq lock. It finds the task no longer throttled, or + * arms the timer again at the next period set here. + */ + cancel_replenish_timer(dl_se); + /* A yield gave up the rest of the old period only. */ + dl_se->dl_yielded = 0; + if (!dl_setattr_place(dl_se, now, &dl_se->period_start, &dl_se->deadline, + &dl_se->runtime, old_runtime, old_deadline, + old_period, !dl_se->dl_throttled && !asleep, asleep)) { + dl_se->dl_throttled = 0; + return false; + } + + /* + * The next period is ahead, so the timer can be armed. The task + * stays off the dl_rq when the change enqueues it again. If it is + * running, reschedule, as a throttle in update_curr_dl_se() does. + */ + dl_se->dl_throttled = 1; + if (WARN_ON_ONCE(!start_dl_timer(dl_se))) + replenish_dl_entity(dl_se); + if (task_current_donor(rq, p)) + resched_curr(rq); + return false; +} + +#ifdef CONFIG_RT_MUTEXES +/* + * If sched_setattr() changed the parameters of @p while @p waits on an + * rt_mutex, check its runtime left against them now, as a wakeup does. + * __sched_setscheduler() calls rt_mutex_adjust_pi() next. That chain walk + * copies the deadline of @p into its waiter node, before any wakeup. Only + * __setscheduler_dl_pi() calls this: other changes of @p do not requeue + * the waiter. + * + * Leave dl_new_params set for a task that is boosted, throttled, or in its + * constrained gap. Its first enqueue after the deboost, its replenishment + * or its wakeup deals with the new parameters. + * + * The caller holds the rq lock and the pi_lock of @p, and has updated the + * rq clock. + */ +void dl_check_blocked_params(struct task_struct *p) +{ + struct sched_dl_entity *dl_se = &p->dl; + + if (!dl_task(p) || task_on_rq_queued(p) || !dl_task_pi_blocked(p)) + return; + if (dl_se->dl_new_params && !is_dl_boosted(dl_se) && + !dl_se->dl_throttled && !dl_in_constrained_gap(dl_se)) { + dl_se->dl_new_params = 0; + update_dl_entity(dl_se); + } +} +#endif /* CONFIG_RT_MUTEXES */ + +/* + * Set the deadline parameters of @p from @attr. A task new to the class + * gets its runtime and its absolute deadline at its first enqueue with + * its new policy. For a task with a period in progress, fit that period + * to the new parameters with dl_setattr_period(). */ void __setparam_dl(struct task_struct *p, const struct sched_attr *attr) { struct sched_dl_entity *dl_se = &p->dl; + u64 period = attr->sched_period ?: attr->sched_deadline; + u64 old_runtime = dl_se->dl_runtime; + u64 old_deadline = dl_se->dl_deadline; + u64 old_period = dl_se->dl_period; + bool pending = dl_se->dl_new_params; bool held; + if (attr->sched_runtime != dl_se->dl_runtime || + attr->sched_deadline != dl_se->dl_deadline || + period != dl_se->dl_period) + dl_se->dl_new_params = 1; + held = dl_se_holds_bw(dl_se); dl_se->dl_runtime = attr->sched_runtime; dl_se->dl_deadline = attr->sched_deadline; @@ -5113,6 +5452,30 @@ void __setparam_dl(struct task_struct *p, const struct sched_attr *attr) else if (!dl_se_holds_bw(dl_se) && held) atomic_dec(&dl_nr_bw_tasks); dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime); + /* + * Fit the period in progress to the new parameters. Then clear + * dl_new_params if the runtime left needs no check. Keep it if it + * was set before this change: that check is still due. + */ + if (dl_se->dl_new_params && + !dl_setattr_period(dl_se, old_runtime, old_deadline, old_period) && + !pending) + dl_se->dl_new_params = 0; + + /* + * The inactive timer of a task asleep in the class is armed for the + * 0-lag time of its old period. dl_change_utilization() cancels it + * when dl_bw changes. sched_dl_overflow() does not call it when dl_bw + * stays the same. Arm the timer again from the period set above. If + * its handler is already running, it releases the bandwidth. + */ + if (dl_prio(p->prio) && !is_dl_boosted(dl_se) && + dl_se->dl_non_contending && + hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1) { + dl_se->dl_non_contending = 0; + put_task_struct(p); + task_non_contending(dl_se, true); + } } void __getparam_dl(struct task_struct *p, struct sched_attr *attr, unsigned int flags) @@ -5224,6 +5587,7 @@ static void __dl_clear_params(struct sched_dl_entity *dl_se) dl_se->dl_defer_running = 0; dl_se->dl_defer_armed = 0; dl_se->dl_bw_oncpu = 0; + dl_se->dl_new_params = 0; } void init_dl_entity(struct sched_dl_entity *dl_se) diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index daee51bcbff0..370626136bd5 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -426,6 +426,8 @@ static inline void dl_set_donor(struct sched_dl_entity *dl_se, dl_replace_donor(dl_se, pi_se); } +extern void dl_check_blocked_params(struct task_struct *p); + /* Make @dl_se its own donor. The caller holds the rq lock of its task. */ static inline void dl_clear_donor(struct sched_dl_entity *dl_se) { diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c index 79285e66e051..5bb43b68653e 100644 --- a/kernel/sched/syscalls.c +++ b/kernel/sched/syscalls.c @@ -242,15 +242,31 @@ static void __setscheduler_params(struct task_struct *p, const struct sched_attr *attr) { int policy = attr->sched_policy; + bool enters_dl; if (policy == SETPARAM_POLICY) policy = p->policy; + enters_dl = dl_policy(policy) && !dl_policy(p->policy); p->policy = policy; - if (dl_policy(policy)) + if (dl_policy(policy)) { + u64 period = attr->sched_period ?: attr->sched_deadline; + bool same = attr->sched_runtime == p->dl.dl_runtime && + attr->sched_deadline == p->dl.dl_deadline && + period == p->dl.dl_period; + __setparam_dl(p, attr); - else if (fair_policy(policy)) + /* + * A task that comes back to the class before its 0-lag time + * still has its old runtime and deadline. With new parameters, + * __setparam_dl() has fitted its period to them. With the same + * ones, set dl_new_params, so that the runtime left is checked + * as at a wakeup. + */ + if (enters_dl && same) + p->dl.dl_new_params = 1; + } else if (fair_policy(policy)) __setparam_fair(p, attr); /* rt-policy tasks do not have a timerslack */ @@ -311,6 +327,9 @@ static inline void __setscheduler_dl_pi(int newprio, int policy, */ if (!dl_prio(newprio)) dl_clear_donor(&p->dl); + + /* Check a blocked task before rt_mutex_adjust_pi() requeues it. */ + dl_check_blocked_params(p); } #else /* !CONFIG_RT_MUTEXES */ static inline void __setscheduler_dl_pi(int newprio, int policy, diff --git a/tools/testing/selftests/sched/deadline/deadline_bw_test.c b/tools/testing/selftests/sched/deadline/deadline_bw_test.c index 165ff98951dc..07beae8ca202 100644 --- a/tools/testing/selftests/sched/deadline/deadline_bw_test.c +++ b/tools/testing/selftests/sched/deadline/deadline_bw_test.c @@ -1193,17 +1193,17 @@ static long long bw_of(uint64_t runtime, uint64_t period) } /* - * A deadline task of 20ms every 100ms runs 10ms of a new period, then - * sleeps. The main thread changes its parameters, then switches it to - * SCHED_OTHER. task_non_contending() computes its 0-lag time from the - * 10ms left and the new parameters. The root domain keeps the new - * bandwidth in its total_bw until then. - * - Changed to 5ms every 100ms, the 10ms left overflow the new - * parameters. The 0-lag time has passed. The bandwidth must be - * released at once. The old parameters would keep it about 40ms more. - * - Changed to 50ms every 100ms, the 10ms left fit. The 0-lag time is - * 20ms before the deadline, about 70ms ahead. The bandwidth must still - * be counted right after the switch, and released after the deadline. + * A deadline task of 20ms every 100ms runs 10ms at the start of a new + * period, then sleeps. In those 10ms, its bandwidth gave it 2ms: it ran + * 8ms ahead. The main thread changes its parameters, then switches it + * to SCHED_OTHER. The change keeps the 8ms as a debt, repaid at the new + * bandwidth. The inactive timer releases the bandwidth once the debt + * is repaid. Until then, the root domain counts the new bandwidth in its + * total_bw: + * - changed to 5ms every 100ms, for about 160ms; + * - changed to 50ms every 100ms, for about 16ms. + * The bandwidth must be counted right after the switch, and released + * within 300ms. */ static int params_switched_asleep(void) { @@ -1228,16 +1228,16 @@ static int params_switched_asleep(void) ret = KSFT_FAIL; } else { bw = total_bw_of(cpu); - want = base + (i ? bw_of(rt[i] * NSEC_PER_MSEC, PERIOD) : 0); + want = base + bw_of(rt[i] * NSEC_PER_MSEC, PERIOD); if (bw != want) { ksft_print_msg("%llums every 100ms: total_bw %lld after the switch, %lld expected\n", (unsigned long long)rt[i], bw, want); ret = KSFT_FAIL; } - sleep_ns(PERIOD); /* past the deadline */ + sleep_ns(3 * PERIOD); /* past the 0-lag time */ bw = total_bw_of(cpu); if (ret == KSFT_PASS && bw != base) { - ksft_print_msg("%llums every 100ms: total_bw %lld after the deadline, %lld expected\n", + ksft_print_msg("%llums every 100ms: total_bw %lld after the 0-lag time, %lld expected\n", (unsigned long long)rt[i], bw, base); ret = KSFT_FAIL; } -- 2.47.3